1

我有一个大数据框(base_cov_norm_compl_taxid3),每一行代表一个基因组区域,每一列代表样本中该区域的覆盖率。每个taxed有多个基因组区域(类似于基因组),我想使用聚合来查找相同类型的所有基因组区域的均值和标准差等。

 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)
4

2 回答 2

1

你可以使用data.table

  • 它经过优化,mean因此速度非常快。
  • subset在调用内定义也很容易。
  • 它具有@Dwinwith解决方案的所有优点,不必用大量$'s污染代码

这是一个例子

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]
于 2012-09-10T23:24:55.053 回答
1
 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-04-17T12:19:51.417 回答