我有一个数据框,其日期在接下来的24个月每个月都会更改,因此我无法在代码中使用列名。我想使用对每一行求和的mutate,因此在此示例中,按行4:6求和。但是,数据帧将有24个月的时间,因此每次运行文件时都很难对其进行更改。
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")
预期输出应为该行每个月列的总和中的一列。
答案 0 :(得分:1)
您可以使用rowSums
,方法是排除不需要的列,而不包括所需的列。
rowSums(df[-c(1:3)])
#or in a dplyr pipe
df %>%
mutate(sums = rowSums(.[-c(1:3)]))