I have a dataframe called AD
with 50 rows and 12 columns
I'd like to add values by row, in order to have:
AD[1,2] <- AD[1,1] + AD[1,2]
AD[1,3] <- AD[1,2] + AD[1,3]
and so on for each row.
I tried the following:
for (i in nrow(AD)) {
for (j in ncol(AD)) {
AD[i,j] <- AD[i,j] + AD[i,j-1] }}
Here an example:
a b c
1 1 2 3
2 2 3 4
3 3 0 5
4 4 5 6
5 5 0 7
6 6 7 8
And I'd like to have:
a b c
1 1 3 6
2 2 5 9
3 3 3 8
4 4 9 15
5 5 5 12
6 6 13 21