[英]Adding columns sums in dataframe row wise
I would like to add the sums of the columns of my dataframe one row at a time. 我想一次将一行数据框的列总和相加。
So for each row, I would like to compute the sum of the columns above it. 因此,对于每一行,我想计算其上方各列的总和。
Is there an elegant way to do this with a combination of colSums and apply (or sapply, rollapply)? 是否有结合colSums并应用(或sapply,rollapply)的优雅方法? I have been trying a couple of combinations of those, but could not quite figure it out. 我一直在尝试将它们组合在一起,但是还不太清楚。
new_df <- apply(data_frame, 2, cumsum)
With dplyr
, we can do 使用dplyr
,我们可以做
library(dplyr)
data %>%
mutate_all(cumsum)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.