我有一个大型数据帧(base_cov_norm_compl_taxid3),每行表示一个基因组区域,每列表示该区域在样本中的覆盖率。每个紫杉科有多个基因组区域(类似于基因组),我想使用聚合来寻找相同类型的所有基因组区域的均值和sd等。
base_cov_norm_compl_taxid3[1:10,1:10]
geneid_stst attr taxid
1 1001585.66299.NC_015410_1089905_1090333 mrkg 1001585
2 1001585.66299.NC_015410_1090348_1090740 mrkg 1001585
3 1001585.66299.NC_015410_1215751_1216851 mrkg 1001585
4 1001585.66299.NC_015410_2346036_2347421 mrkg 1001585
5 1001585.66299.NC_015410_2354962_2429569 PFPR 1001585
6 1001585.66299.NC_015410_2610633_2611913 mrkg 1001585
7 1001585.66299.NC_015410_3224232_3225248 mrkg 1001585
8 1001585.66299.NC_015410_3682375_3683115 mrkg 1001585
9 1001585.66299.NC_015410_4101816_4103195 mrkg 1001585
10 1001585.66299.NC_015410_4141587_4142873 mrkg 1001585
locus X765560005.stool1 X764224817.stool1 MH0008
1 NC_015410_1089905_1090333 0 0 0.0000000000
2 NC_015410_1090348_1090740 0 0 0.0000000000
3 NC_015410_1215751_1216851 0 0 0.0000000000
4 NC_015410_2346036_2347421 0 0 0.0281385281
5 NC_015410_2354962_2429569 0 0 0.0005361355
6 NC_015410_2610633_2611913 0 0 0.0000000000
7 NC_015410_3224232_3225248 0 0 0.0000000000
8 NC_015410_3682375_3683115 0 0 0.0000000000
9 NC_015410_4101816_4103195 0 0 0.0000000000
10 NC_015410_4141587_4142873 0 0 0.0000000000
V1.CD9.0 X764062976.stool1 X160643649.stool1
1 0.0000000000 0 0
2 0.0000000000 0 0
3 0.0000000000 0 0
4 0.0000000000 0 0
5 0.0004557152 0 0
6 0.0000000000 0 0
7 0.0000000000 0 0
8 0.0000000000 0 0
9 0.0000000000 0 0
10 0.0000000000 0 0mrkg类型的基因组总是有多个区域,有时每个基因组有多个PFPR区。我想按taxid和attr进行聚合,但是只针对那些使用attr=mrkg的人。我不知道该怎么做。下面的代码可以按taxid和attr进行聚合,但我想先编写list(base_cov_norm_compl_taxid3$taxid,base_cov_norm_compl_taxid3$attr=mrkg)或一些子集?
感谢您的任何帮助,
base_cov_mean<-aggregate(base_cov_norm_compl_taxid3[,5:266],
list(base_cov_norm_compl_taxid3$taxid,
base_cov_norm_compl_taxid3$attr),mean)发布于 2012-04-17 20:19:51
subdf <- subset(base_cov_norm_compl_taxid3, attr %in% "mrkg")
base_cov_mean <- with(subdf, aggregate(subdf[5:266],
by=list(taxid, attr),
FUN=mean)
)我没有使用attr == "mrkg",因为它也不能泛化。
发布于 2012-09-11 07:24:55
您可以使用data.table
mean进行了优化,所以会非常快。subset也很容易。with解决方案的所有优点,而不必使用大量的$的来污染代码
下面是一个例子
library(data.table)
DT <- data.table( base_cov_norm_compl_taxid3)
# the columns of which you want the eman
columns_of_interest <- names(DT)[5:266]
DT[attr %in% 'mrkg', lapply(.SD, mean), by = list(taxid, attr), .SDcols = columns_of_interest]https://stackoverflow.com/questions/10190490
复制相似问题