2
votes

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.

2

2 Answers

2
votes
new_df <- apply(data_frame, 2, cumsum)
1
votes

With dplyr, we can do

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