我有BLAST HSP的数据框(并非显示所有列):
query.id subject.id alignment.length
196 1032519524 1032519523 212
197 1032519524 1032519523 182
198 1032519524 1032519522 212
199 1032519524 1032519522 182
200 1032519524 1032518642 212
并且想要折叠数据,所以我最终使用唯一的对query.id和subject.id。如果有多个行具有相同的query.id和subject.id,则应添加alignment.length的值:
query.id subject.id alignment.length
1 1032519524 1032518642 212
2 1032519524 1032519522 394
3 1032519524 1032519523 394
我使用plyr
:
ddply(blast.results, c("query.id", "subject.id"), function(x)colSums(x['alignment.length']))
不幸的是,在处理数十万个BLAST结果时,这变得令人望而却步。是否有更快,更具可扩展性的方法?
@PoGibas data.table解决方案的Microbenchmark:
Unit: milliseconds
expr
setDT(blast.results)[, .(alignment.length = sum(alignment.length)), .(query.id, subject.id)]
setkey(setDT(blast.results), query.id, subject.id)[, .(alignment.length = sum(alignment.length)), .(query.id, subject.id)]
100
min lq mean median uq max neval cld
11.514016 18.010048 31.61341262 22.0045935 32.104018 222.943783 100 b
15.308905 22.620595 36.32531007 28.2132725 43.527390 156.109477 100 b
0.000012 0.000185 0.00033057 0.0003635 0.000443 0.000772 100 a
答案 0 :(得分:1)
library(dplyr)
blast.results %>%
group_by(query.id, subject.id) %>%
summarise(alignment.length = sum(alignment.length))
使用data.table
(@Matt Dowle)的解决方案:
library(data.table)
setkey(setDT(blast.results), query.id, subject.id)[, .(alignment.length = sum(alignment.length)), .(query.id, subject.id)]
如你所说速度很重要,那么你可能想要使用data.table
(data.table
vs dplyr
)。