I am trying to remove a row from my dataframe in every iteration in a for loop and perform correlation test on the newly saved dataframe. However, I am not getting what I expect. Please help. Each row in the dataframe provided represents corresponding column name data of an individual.
rnpo <- data.frame(h.move.ten = c(25.85, 51.375, 26.007, 35.249, 30.841), move.ten = c(3.231, 0.000, 4.334, 4.745, 0.000), reor.ten = c(0.000, 3.626, 1.181, 2.027, 2.457), hbob.ten = c(3.398, 17.934, 7.050, 1.075, 0.943))
store.cor <- numeric(nrow(rnpo))
for (i in 1:nrow(rnpo)) {
droprow <- rnpo[-i,]
store.cor[i] <- cor(droprow)
}
This is the code that I am trying to use.
Alternatively, I am trying to use:
store.cor <- numeric(nrow(rnpo))
data.ind <- 1:nrow(rnpo)
store.cor <- sapply(data.ind, function(x) cor(rnpo[-x]))
calc.cor <- function(x,vec) {
cor(vec[-x])
}
store.cor <- sapply(data.ind, calc.cor, vec=rnpo)
store.cor
Here, my columns are getting dropped in every iteration instead of my rows. How to fix this problem?