I am very new to R.
I have a large data frame and I need to sum the row values in a particular fashion and create a new data frame with the results. For example, my input data frame is something like:
f | avg | set | other |
---|---|---|---|
r1 | 2 | 6 | 1 |
r2 | 4 | 12 | 2 |
r3 | 3 | 27 | 3 |
r4 | 1 | 3 | 2 |
I need to create a new data frame with each row adding the 'avg' value from the following row. To make it more clear: My output would look something like:
file | sum_avg |
---|---|
r1_2 | 6 |
r1_2_3 | 9 |
r1_2_3_4 | 10 |
Where r1_2 is the sum 'avg' in rows 1 and 2; r1_2_3 is the sum of 'avg' in rows1, 2 and 3; r1_2_3_4 is the sum of 'avg' for in rows 1, 2, 3 and 4... and so on... I have several rows (40). I would like a row name that represents the value (like r1_2, r1_2_3, r1_2_3_4 or similar.
I tried to do it manually but it is very error prone and takes forever to write. Can someone please suggest a cleaner way to do it?
CodePudding user response:
INPUT is your input table. I added columns with the wanted results.
Create the row names for the output by using the paste function
INPUT$file <- paste0("r",sapply(1:nrow(INPUT),function(X) paste0(1:X,collapse = "_")))
use built in function
INPUT$sum_avg <- cumsum(INPUT$avg)
result
INPUT f avg set other file sum_avg 1 r1 2 6 1 r1 2 2 r2 4 12 2 r1_2 6 3 r3 3 27 3 r1_2_3 9 4 r4 1 3 2 r1_2_3_4 10