Adding columns sums in dataframe row wise

三世轮回 提交于 2019-12-11 03:07:36

问题


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)? I have been trying a couple of combinations of those, but could not quite figure it out.


回答1:


new_df <- apply(data_frame, 2, cumsum)



回答2:


With dplyr, we can do

library(dplyr)
data %>%
      mutate_all(cumsum)


来源:https://stackoverflow.com/questions/48603696/adding-columns-sums-in-dataframe-row-wise

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!