2

I am using R-Studio the latest version.

I have created a bunch of data frames that I would like to test for correlation. After creating the data frames I created a variable u that denotes the universe of all the data frames. I would like to create a loop that will go through each data.frame in u and do the following test corr(data.frame)

I have the following code:

corrvals <- NULL
for (i in seq(along=u[])) {
corrvals <- corr(u)
}

I found something sort of along the lines of what I want to do here

The thing is, all the data.frame's are setup exactly how I want them, and I simply would like to run though every data.frame in the list and run the corr function on it.

I would also like to print out the name of the data.frame and its correlation value, as so:

data.frame Corr
ac         -0.03445345
af          0.023429
.
.
.
n           corr(n)

into my empty storage container corrvals.

Thank You

Community
  • 1
  • 1
MCP_infiltrator
  • 3,961
  • 10
  • 45
  • 82

2 Answers2

4

I suggest to put your data.frames into a list and then run lapply. Like this:

# setting up a reproducible example
data(mtcars)
data(iris)
# remove the last column cause it ain't numeric.
iris <- iris[,-5]
listOfDataFrames <- list()
listOfDataFrames[[1]] <- mtcars
listOfDataFrames[[2]] <- iris

# here's a one liner using base R. 
lapply(listOfDataFrames,cor)

Welcome to SO, btw. Ah, I guess you mean cor, right? However this works with basically any function.

Matt Bannert
  • 27,631
  • 38
  • 141
  • 207
4

I seem to post a lot of lapply on here. It's a convenient wrapper that hides the loop, but does exactly what you want...

Edit

A little more involved as you want the names. Also the corr function is from package boot:

u <- list( df1 , df2 , df3 )
attr(u, "names") <- c("df1","df2","df3")
require( boot )
res <- lapply( u , function(x) { names(x) ; corr(x) } )

res
#$df1
#[1] 0.353647

#$df2
#[1] 0.4494202

#$df3
#[1] -0.003907642
Community
  • 1
  • 1
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184