I have a dataframe
# Create an empty dataframe to store the results
result_df <- data.frame()
test_data <- as.data.frame(list(V1 = c(6, 15, 14, 13, 5),
V2 = c(14, 18, 5, 2, 16),
V3 = c(11, 20, 19, 18, 10),
V4 = c(22, 22, 11, 18, 15),
V5 = c(-8, -12, 1, 4, -10)))
Then I check every column for correlation
# Loop through each column
for (i in 1:(ncol(test_data) - 1)) {
for (j in (i + 1):ncol(test_data)) {
# Compare columns (example: calculate mean difference)
result <- cor(test_data[, i], test_data[, j])
# Append the result to the result dataframe
result_df <- rbind(result_df,
data.frame(Column1 = names(test_data)[i],
Column2 = names(test_data)[j],
Result = result))
}
}
# Print the result dataframe
result_df$Result <- abs(result_df$Result)
And then I need to subset the res
. For some reason it shows only one row, while there are two.
res <- result_df[result_df$Result==1,]
When I use isTRUE(all.equal(result_df$Result, 1))
the result is even worse and shows no data.
# res <- result_df[isTRUE(all.equal(result_df$Result, 1)),]
Why?