hawkeye03
hawkeye03

Reputation: 27

How to use cor.test in an apply function?

I have a really large csv file (dat2) and I'm trying to use cor.test to get a p value comparing one column (Age) to multiple other columns (every column after Age). Then I need to print the p value. I got it to work using a for loop, but it takes a really long time. I want to use an apply function because I think it will shorten the compile time?

The ## portion is the for loop that works.

b <- apply(dat2[,-1], 1(,4:ncol(dat2)), cor.test(dat2(Age), method="pearson", use="pairwise"))
sapply(b, "[[", "p.value")

## for (i in 4:ncol(dat2)) {
## a <- cor.test(dat2[,3], dat2[,i], method="pearson", use="pairwise")
## print(paste(colnames(dat2)[i], " p=value:", a$p.value))
## }

Upvotes: 1

Views: 2670

Answers (1)

nsinghphd
nsinghphd

Reputation: 2022

You were on the right track but with few mistakes. Check the following code, I believe it produces your desired output

b = apply(df2[, -1], 2, function(x) {
    cor.test(df2[, 1], x, method = "pearson", use = "pairwise")
})

p.vals <- sapply(b, "[[", "p.value")
p.vals

country   value 
      0       0 

Upvotes: 1

Related Questions