I have a dataframe with gene expression data by lane (column). What I would like to do is write a loop that takes the sum of each row but progressively adds in another column each time. So each time I loop through I add another column to my dataframe that contains the sums of each row plus another column to the end of the dataframe. In the example below I did this using the apply() function by hand but this is very inefficient and not feasible for a large data set. I messed around with the cumsum() function but couldn't seem to get it to work for this. Very possible I missed something obvious but any guidance would be great!
#Example dataframe
c1 <- c('G1', 'G2', 'G3') c2 <- c(5, 3, 1) c3 <- c(3, 7, 1) c4 <- c(6, 3, 4) c5 <- c(6, 4, 3) df <- data.frame(c1, c2, c3, c4, c5) #Cal cumulative sums sum.2.3 <- apply(df[,2:3],1,sum) sum.2.4 <- apply(df[,2:4],1,sum) sum.2.5 <- apply(df[,2:5],1,sum) df <- cbind(df, sum.2.3, sum.2.4, sum.2.5)