user3631848
user3631848

Reputation: 483

To get the sum for every 2 rows in R

My data has only one column. Then I need to get the sum for every two rows from the beginning. I have read the file by read.table function. The data looks like this

   V1
1  17059177
2  17059177
3  17214138
4  17214138

Please help. I prefer to try apply function. Thank you.

Upvotes: 0

Views: 3269

Answers (1)

akrun
akrun

Reputation: 887048

We can use rowsum to get the sum of every 2 elements in the 'V1' column

rowsum(df1[,1], as.integer(gl(nrow(df1), 2, nrow(df1))))

Upvotes: 4

Related Questions