I want to adapt the method in cor.test in R for each data frame in a list of data frames.
data(iris)
iris.lst <- split(iris[, 1:2], iris$Species)
options(scipen=999)
normality1 <- lapply(iris.lst, function(x) shapiro.test(x[,1]))
p1 <- as.numeric(unlist(lapply(normality1, "[", c("p.value"))))
normality2 <- lapply(iris.lst, function(x)shapiro.test(x[,2]))
p2 <- as.numeric(unlist(lapply(normality2, "[", c("p.value"))))
try <- ifelse (p1 > 0.05 | p2 > 0.05, "spearman", "pearson")
# Because all of them are spearman:
try[3] <- "pearson"
for (i in 1: length(try)){
results.lst <- lapply(iris.lst, function(x) cor.test(x[, 1], x[, 2], method=try[i]))
results.stats <- lapply(results.lst, "[", c("estimate", "conf.int", "p.value"))
stats <- do.call(rbind, lapply(results.stats, unlist))
stats
}
But it does not compute for each data frame individual cor.test...
cor.test(iris.lst$versicolor[, 1], iris.lst$versicolor[, 2], method="pearson")`
stats
# Should be spearman corr.coefficient but is pearson
Any advice?