[英]Looping over a list of data frames and calculate the correlation coefficient
I am using R-Studio the latest version. 我正在使用R-Studio最新版本。
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. 创建数据帧后,我创建了一个变量u
,表示所有数据帧的Universe。 I would like to create a loop that will go through each data.frame
in u
and do the following test corr(data.frame)
我想创建一个循环来data.frame
u
每个data.frame
并执行以下测试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. 问题是,所有data.frame
的设置都是我想要的,我只是希望运行列表中的每个data.frame
在其上运行corr
函数。
I would also like to print out the name of the data.frame and its correlation value, as so: 我还想打印出data.frame的名称及其相关值,如下所示:
data.frame Corr
ac -0.03445345
af 0.023429
.
.
.
n corr(n)
into my empty storage container corrvals
. 进入我的空储存容器corrvals
。
Thank You 谢谢
I suggest to put your data.frames into a list and then run lapply
. 我建议把你的data.frames放到一个列表中,然后运行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. 欢迎来到SO,顺便说一句。 Ah, I guess you mean cor
, right? 啊,我想你的意思是cor
,对吗? However this works with basically any function. 然而,这基本上适用于任何功能。
I seem to post a lot of lapply
on here. 我似乎在这里发布了很多lapply
。 It's a convenient wrapper that hides the loop, but does exactly what you want... 这是一个方便的包装器隐藏循环,但完全符合您的要求......
A little more involved as you want the names. 如果你想要名字,可以多一点参与。 Also the corr
function is from package boot
: corr
函数也来自包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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.