My goal is to sum certain columns of my dataframe and input that sum into a new column.
Suppose I have the following data.frame:
df <- data.frame(names=c("a","b","c","d","e","f"),
                 wb01=c(1,1,0,1,1,0),
                 wb02=c(0,0,0,0,1,1),
                 wb03=c(0,0,1,1,1,1),
                 wb04=c(1,1,0,1,1,1),
                 wb05=c(1,0,1,0,0,1),
                 wb06=c(1,1,1,1,1,1)) 
rownames(df) <- df$names
  wb01 wb02 wb03 wb04 wb05 wb06
a  1    0    0    1    1    1
b  1    0    0    1    0    1
c  0    0    1    0    1    1
d  1    0    1    1    0    1
e  1    1    1    1    0    1
f  0    1    1    1    1    1
I would like to select what columns are to be summed by using a vector that will contain the names of the columns to be sum. (My real dataframe and the number of columns I will be choosing is quite large and not in bunched together, ie/ I can't just choose columns 3-5, nor do I want to type each column since it would be over 2k...)
But back to the example, here are the columns I'd like to sum:
genelist <- c(wb02, wb03, wb06)
So the results would look like this:
  wb01 wb02 wb03 wb04 wb05 wb06 sum_genelist
a  1    0    0    1    1    1         1
b  1    0    0    1    0    1         1
c  0    0    1    0    1    1         2
d  1    0    1    1    0    1         3
e  1    1    1    1    0    1         3
f  0    1    1    1    1    1         3
Thanks for any help or tips!
 
     
    