-5

I have a large data dataframe (48 x 100). Is there an elegant formula in R that makes you transform this dataframe in to a "custom dataframe"?

a = c(2, 3, 5)
b = c(2, 3, 5)
c = c(2, 3, 5)

df <- rbind(a,b,c)

Now.. i want cumsum of df so it looks like this.

desired output

I know its easy to do with a loop.. but is there a function?

f.lechleitner
  • 3,554
  • 1
  • 17
  • 35
  • Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Jan 03 '18 at 12:11
  • Also, see `?cumsum` – Jaap Jan 03 '18 at 12:12
  • yes.. i want cumsum on a large number of columbs. I could just make a loop... but the question is there a more elegant way of doing htis? – Soren Christensen Jan 03 '18 at 12:14
  • So you want the results in a matrix not a data frame? Please paste the results into a code block not link an image. Also explain what code you have tried. – Elin Jan 03 '18 at 12:21
  • i was not being precise.. dataframe – Soren Christensen Jan 03 '18 at 12:22
  • The code above creates a matrix not a data frame. See the answer below for the additional step needed. You should either clarify the title of clarify the problem you had. – Elin Jan 03 '18 at 12:35

1 Answers1

4

Like this:

a <- c(2, 3, 5)
b <- c(2, 3, 5)
c <- c(2, 3, 5)

df <- data.frame(rbind(a,b,c))
df <- cumsum(df)

so this dataframe:

> df
  X1 X2 X3
a  2  3  5
b  2  3  5
c  2  3  5

becomes this:

> cumsum(df)
  X1 X2 X3
a  2  3  5
b  4  6 10
c  6  9 15
f.lechleitner
  • 3,554
  • 1
  • 17
  • 35