1

I have a dataframe with dates that will change everymonth for the next 24 months, so I can't use the column names in my code. I want to use mutate that sums each row, so in this example sum by row columns 4:6. However, the dataframe will have 24 months, so it's not convenient to change it each time I run the file.

df <- structure(list(Region = c("Region 1", "Region 1", "Region 1","Region 1", "Region 1", "Region 1", "Region 1", "Region 1", "Region 1"), Customer = c("Customer A", "Customer B", "Customer C", "Customer D","Customer E", "Customer F", "Customer G", "Customer H", "Customer J"), Market = c("Market #1", "Market #2", "Market #3", "Market #4","Market #5", "Market #6", "Market #7", "Market #8", "Market #9"), `19-Jun` = c(21408912L, 10222308L, 9600000L, 524753L, 4200000L,529373L, 4100000L, 3391500L, 3834617L), `19-Jul` = c(33405963L,24306609L, 11528470L, 683497L, 6900000L, 763390L, 4100000L, 3510500L,3500000L), `19-Aug` = c(32784032L, 22389084L, 11528470L, 859897L,7500000L, 768152L, 4100000L, 3510500L, 4000000L)), row.names = c(NA,9L), class = "data.frame")

The expected output should be a column of the sum of each month column in the row.

Sotos
  • 51,121
  • 6
  • 32
  • 66
  • what do you mean by `expected output should be a column of the sum of each month column in the row.` ? Do you want `rowSums(df[4:6])` ? – Ronak Shah Nov 07 '19 at 13:24
  • 1
    or maybe `rowSums(df[-c(1:3)])` since you will eventually have additional columns...? – Sotos Nov 07 '19 at 13:28
  • `rowSums(df[!names(df) %in% c("Region", "Market", "Customer")])` if the number of columns will change but the keys will not – Chris Littler Nov 07 '19 at 13:28
  • I would like to use this in a dplyr pipe, so I'd like to use mutate to add a column, i.e. sum_of_row that would be the sum of all the month columns for each row. – Jeremy R. Johnson Nov 07 '19 at 13:49
  • @Sotos comment seems to work fine, but I don't know how to put that into a dplyr pipe. – Jeremy R. Johnson Nov 07 '19 at 13:53
  • 1
    Something like `df %>% mutate(sums = rowSums(.[-c(1:3)]))` – Sotos Nov 07 '19 at 13:56
  • @Sotos, If you'll put that in the form of an answer, I'll mark it as the correct answer. That is exactly what I needed. Thank you very much, especially for your quick response. – Jeremy R. Johnson Nov 07 '19 at 14:38
  • Similar posts https://stackoverflow.com/q/29006056/5325862 and https://stackoverflow.com/q/37085389/5325862 – camille Nov 07 '19 at 14:49

1 Answers1

2

You can use rowSums by excluding the columns you don't need rather than including the ones you need.

rowSums(df[-c(1:3)])

#or in a dplyr pipe 
df %>% 
 mutate(sums = rowSums(.[-c(1:3)]))
Sotos
  • 51,121
  • 6
  • 32
  • 66