[英]lapply and for loop to run a function through a list of data.frames in R
I have a list of data.frame and I'd like to run cor.test
through each data.frame. 我有data.frame的名单,我想运行cor.test
通过每个data.frame。 The data.frame
has 8 columns, I would like to run cor.test
for each of the first 7 columns against the 8th column. data.frame
有8列,我想为第8列的前7列中的每一列运行cor.test
。
I first set up the lists for storing the data 我首先设置了用于存储数据的列表
estimates = list()
pvalues = list()
Then here's the loop combining with lapply
然后这是与lapply
结合的循环
for (i in 1:7){
corr <- lapply(datalist, function(x) {cor.test(x[,i], x[,8], alternative="two-sided", method="spearman", exact=FALSE, continuity=TRUE)})
estimates= corr$estimate
pvalues= corr$p.value
}
It ran without any errors but the estimates
shows NULL
它运行没有任何错误,但estimates
显示NULL
Which part of this went wrong? 哪一部分出了问题? I used to run for
loop over cor.test
or run is with lapply
, never put them together. 我用来运行for
遍历cor.test
或运行与lapply
,从来没有把它们放在一起。 I wonder if there's a solution to this or an alternative. 我想知道是否有解决方案或替代方案。 Thank you. 谢谢。
We can use sapply
, showing with an example on mtcars
where cor.test
is performed with all columns against the first column. 我们可以使用sapply
,在mtcars
上显示一个示例,其中cor.test
与第一列的所有列cor.test
执行。
lst <- list(mtcars, mtcars)
lapply(lst, function(x) t(sapply(x[-8], function(y) {
val <- cor.test(y, x[[8]], alternative ="two.sided",
method="spearman", exact=FALSE, continuity=TRUE)
c(val$estimate, pval = val$p.value)
})))
[[1]]
# rho pval
#mpg 0.7065968 6.176953e-06
#cyl -0.8137890 1.520674e-08
#disp -0.7236643 2.906504e-06
#hp -0.7515934 7.247490e-07
#drat 0.4474575 1.021422e-02
#wt -0.5870162 4.163577e-04
#qsec 0.7915715 6.843882e-08
#am 0.1683451 3.566025e-01
#gear 0.2826617 1.168159e-01
#carb -0.6336948 9.977275e-05
#[[2]]
# rho pval
#mpg 0.7065968 6.176953e-06
#cyl -0.8137890 1.520674e-08
#.....
This returns you list of two column matrix with estimate
and p.value
respectively. 这将返回两个列矩阵列表,分别包含estimate
和p.value
。
Disclaimer : This answer uses the developer version of manymodelr that I also wrote. 免责声明 :这个答案使用了我也写过的manymodelr的开发者版本。
EDIT : You can map it to your list of data frames with Map
or lapply
for instance: 编辑 :您可以使用Map
或lapply
将其映射到数据框列表:
lst <- list(mtcars, mtcars) #Line copied and pasted from @Ronak Shah's answer
Map(function(x) manymodelr::get_var_corr(x, "mpg",get_all = TRUE,
alternative="two.sided",
method="spearman",
continuity=TRUE,exact=F),lst)
For a single data.frame
object, we can use get_var_corr
: 对于单个data.frame
对象,我们可以使用get_var_corr
:
manymodelr::get_var_corr(mtcars, "mpg",get_all = TRUE,
alternative="two.sided",
method="spearman",
continuity=TRUE,exact=FALSE)
# Comparison_Var Other_Var p.value Correlation
# 1 mpg cyl 4.962301e-13 -0.9108013
# 2 mpg disp 6.731078e-13 -0.9088824
# 3 mpg hp 5.330559e-12 -0.8946646
# 4 mpg drat 5.369227e-05 0.6514555
# 5 mpg wt 1.553261e-11 -0.8864220
# 6 mpg qsec 7.042244e-03 0.4669358
# 7 mpg vs 6.176953e-06 0.7065968
# 8 mpg am 8.139885e-04 0.5620057
# 9 mpg gear 1.325942e-03 0.5427816
# 10 mpg carb 4.385340e-05 -0.6574976
purrr has some convenience functions could possibly make this operation a little more simple (although its debatable whether this is actually simpler than the Map/lapply way). purrr有一些方便的功能可能会使这个操作变得更简单一些(尽管这是否比Map / lapply方式更简单,这是有争议的)。 Using Ronak's example list lst
: 使用Ronak的示例列表lst
:
library(purrr)
lst <- list(mtcars, mtcars)
map2(map(lst, ~.[-8]), map(lst, 8), ~
map(.x, cor.test, y = .y,
alternative = "two.sided",
method = "spearman",
exact = FALSE,
continuity = TRUE) %>%
map_dfr(extract, c('estimate', 'p.value'), .id = 'var'))
# [[1]]
# # A tibble: 10 x 3
# var estimate p.value
# <chr> <dbl> <dbl>
# 1 mpg 0.707 0.00000618
# 2 cyl -0.814 0.0000000152
# 3 disp -0.724 0.00000291
# 4 hp -0.752 0.000000725
# 5 drat 0.447 0.0102
# 6 wt -0.587 0.000416
# 7 qsec 0.792 0.0000000684
# 8 am 0.168 0.357
# 9 gear 0.283 0.117
# 10 carb -0.634 0.0000998
#
# [[2]]
# # A tibble: 10 x 3
# var estimate p.value
# <chr> <dbl> <dbl>
# 1 mpg 0.707 0.00000618
# 2 cyl -0.814 0.0000000152
# 3 disp -0.724 0.00000291
# 4 hp -0.752 0.000000725
# 5 drat 0.447 0.0102
# 6 wt -0.587 0.000416
# 7 qsec 0.792 0.0000000684
# 8 am 0.168 0.357
# 9 gear 0.283 0.117
# 10 carb -0.634 0.0000998
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.