对data.frame中的后续条目(复制)求和

对data.frame中的后续条目(复制)求和,r,sum,row,apply,R,Sum,Row,Apply,我有一个很简单的问题,我自己找不到简单的解决办法。 我有一个data.frame表达式数据。每行对应一个测量基因。这些列是在不同时间点测量的表达式,其中每个时间点有4个副本。看起来有点像这样: 0h_1 0h_2 0h_3 0h_4 1h_1 1h_2 1h_3 1h_4 2h_1 2h_2 2h_3 2h_4 3h_1 3h_2 3h_3 3h_4 gene1 434

我有一个很简单的问题,我自己找不到简单的解决办法。 我有一个data.frame表达式数据。每行对应一个测量基因。这些列是在不同时间点测量的表达式,其中每个时间点有4个副本。看起来有点像这样:

         0h_1    0h_2    0h_3    0h_4    1h_1   1h_2    1h_3   1h_4    2h_1    2h_2    2h_3     2h_4    3h_1     3h_2     3h_3    3h_4 
gene1    434     123     42      94      9811   262     117    42      327     367     276      224
gene2    47      103     30      847     13     291     167    358     303     293     2263     741
gene3    322     27      97      217     223    243     328    308     328     299     518      434
我想对每一行的所有复制进行汇总,这样每个基因都有一行,每个时间点只有一列,而不是四列。 有没有什么功能可以让我高效地做到这一点

澄清一下:我要找的是这样的data.frame:

         0h     1h     2h     3h     ...
gene1   693     9811  
gene2   1027    13
gene3 

提前谢谢。最好的,Jonas

正如@AntoniosK所建议的,我们可以使用
摘要
而不是
不同的
选择(-iter,-value)

库(dplyr)
df%%>%聚集(键、值、-name)%%>%
分离(键,into=c('timepoint','iter'),sep=''.''''''>%
分组依据(名称、时间点)%>%总结(总结=总结(值,na.rm=TRUE))%>%
价差(时点、总和)
#一个tibble:3x4
#分组:名称[3]
名称X0h X1h X2h
1基因1693 10232 1194
2 Gene210278293600
3 gene3 663 1102 1579
数据
df正如@AntoniosK所建议的,我们可以使用
summary
而不是
distinct
select(-iter,-value)

库(dplyr)
df%%>%聚集(键、值、-name)%%>%
分离(键,into=c('timepoint','iter'),sep=''.''''''>%
分组依据(名称、时间点)%>%总结(总结=总结(值,na.rm=TRUE))%>%
价差(时点、总和)
#一个tibble:3x4
#分组:名称[3]
名称X0h X1h X2h
1基因1693 10232 1194
2 Gene210278293600
3 gene3 663 1102 1579
数据
df在base
R
中有一个选项:

res <- as.data.frame(lapply(split.default(df1, sub("_.*$","",names(df1))), rowSums))
names(res) <- gsub("^X","",names(res))
res
#         0h    1h   2h
# gene1  693 10232 1194
# gene2 1027   829 3600
# gene3  663  1102 1579

res在base
R
中有一个选项:

res <- as.data.frame(lapply(split.default(df1, sub("_.*$","",names(df1))), rowSums))
names(res) <- gsub("^X","",names(res))
res
#         0h    1h   2h
# gene1  693 10232 1194
# gene2 1027   829 3600
# gene3  663  1102 1579

res所有时间点的所有复制?比如每行中所有值的总和?那么,上面示例的输出将是1列3个值?类似于
rowSums(df)
?不,只是每个时间点的复制。因此,结果将为每个基因提供一行,为每个时间点提供一列。请显示您想要的输出!i、 编辑您的问题:所有时间点的所有复制?比如每行中所有值的总和?那么,上面示例的输出将是1列3个值?类似于
rowSums(df)
?不,只是每个时间点的复制。因此,结果将为每个基因提供一行,为每个时间点提供一列。请显示您想要的输出!i、 编辑你的问题:lappy和split.default的组合让我找到了我想要的解决方案,因为我的真实数据有不同的命名。非常感谢您提供此方法!lapply和split.default的组合让我找到了我一直在寻找的解决方案,因为我的真实数据具有不同的命名。非常感谢您提供此方法!感谢您提供的解决方案,但我不想使用dplyr,因为我运行的脚本中不需要它,如果我打算与同事共享代码,我也不想将其作为必需。感谢您提供的解决方案,但我宁愿不使用dplyr,因为我运行的脚本中的任何其他内容都不需要它,如果我打算与同事共享我的代码,我也不希望将它作为要求。
res <- as.data.frame(lapply(split.default(df1, sub("_.*$","",names(df1))), rowSums))
names(res) <- gsub("^X","",names(res))
res
#         0h    1h   2h
# gene1  693 10232 1194
# gene2 1027   829 3600
# gene3  663  1102 1579
df1 <- read.table(text="
0h_1    0h_2    0h_3    0h_4    1h_1   1h_2    1h_3   1h_4    2h_1    2h_2    2h_3     2h_4 
gene1    434     123     42      94      9811   262     117    42      327     367     276      224
gene2    47      103     30      847     13     291     167    358     303     293     2263     741
gene3    322     27      97      217     223    243     328    308     328     299     518      434
",header=T)

names(df1) <- gsub("^X","",names(df1))
df1
#       0h_1 0h_2 0h_3 0h_4 1h_1 1h_2 1h_3 1h_4 2h_1 2h_2 2h_3 2h_4
# gene1  434  123   42   94 9811  262  117   42  327  367  276  224
# gene2   47  103   30  847   13  291  167  358  303  293 2263  741
# gene3  322   27   97  217  223  243  328  308  328  299  518  434