2

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.

Niccola Tartaglia
  • 1,537
  • 2
  • 26
  • 40

2 Answers2

2
new_df <- apply(data_frame, 2, cumsum)
AidanGawronski
  • 2,055
  • 1
  • 14
  • 24
1

With dplyr, we can do

library(dplyr)
data %>%
      mutate_all(cumsum)
akrun
  • 874,273
  • 37
  • 540
  • 662