I have a data.frame in R that looks like
X1 X2 X3 X4
11 2 3 4
2 5 7 1
12 4 7 6
3 2 9 6
I want to sequentially merge every set of two rows into one by taking the sum of their values in each column.
X1 X2 X3 X4
13 7 10 5
15 6 16 12
Here 13 = 11 + 2
, 7 = 2 + 5
, 10 = 3 + 7
and 5 = 4 + 1
. I have no clue how can I do this in R. Can we use rbind
or some similar function to achieve this in one or two lines of code?