[英]How do I refer to all components of a list with a single function in R?
I have split my df into a list containing 500 groups thus: 我将我的df拆分成一个包含500个组的列表:
c1=cut(SNP_Allele_Frequency$College_SE,500)
splitc1=split(SNP_Allele_Frequency,c1,drop=FALSE)
I need to find the mean for a variable for all the 500 groups (levels) contained in the list CL. 我需要找到列表CL中包含的所有500个组(级别)的变量的均值。 Instead of repeating the process 500 times (as below), is there a way I can do this with one function?
而不是重复过程500次(如下所示),有没有办法可以用一个函数做到这一点?
mean(splitc1[[1L]]$ACB)....mean(splitc1[[2L]]$ACB)...
mean(splitc1[[500L]]$ACB)
First let's make some reproducible data: 首先让我们制作一些可重复的数据:
set.seed(24)
SNP_Allele_Frequency <- data.frame(College_SE = rnorm(1000), ACB = rnorm(1000))
Now using your original method: 现在使用您的原始方法:
c1 <- cut(SNP_Allele_Frequency$College_SE, 50)
splitc1 <- split(SNP_Allele_Frequency, c1, drop = FALSE)
lapply(splitc1, function(x) mean(x[["ACB"]]))
We could do it more cleanly in dplyr
: 我们可以在
dplyr
更干净地dplyr
:
library(dplyr)
SNP_Allele_Frequency %>% mutate(c1 = cut(SNP_Allele_Frequency$College_SE, 50)) %>%
group_by(c1) %>%
summarise(meanACB = mean(ACB))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.