R聚集在部分列表中
问题描述:
我有一个很大的数据框(base_cov_norm_compl_taxid3),每一行代表一个基因组区域,每一列代表样本中该区域的覆盖范围。每个taxid有多个基因组区域(类似于基因组),我想使用聚合来找到相同类型的所有基因组区域的手段和sd等。R聚集在部分列表中
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 0
有时总是的mrkg
类型的多个基因组区域,并且有时有每基因组的多个区域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)
答
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"
,因为它并不能一概而论为好。
答
你可以使用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]
感谢 - 是有道理的,但我得到这个错误“FUN(X [[1L]],...)中的错误:参数必须具有相同的长度” – user1249760 2012-04-17 12:26:10
对不起,忘记缩短副向量。看看修补程序是否会起作用。 – 2012-04-17 12:27:48
我没有看到修复 – user1249760 2012-04-17 12:34:29