For example, I have a data frame where I want to get the sum of all columns beginning with "Q3", I could type out each one, but it would be great to be able to do something like this:
#Example data
test <- structure(list(UserID = c("53017366", "53017366"), Q3_1 = c(3L,
3L), Q3_2 = c(2L, 2L), Q3_3 = c(3L, 3L), Q3_4 = c(NA, 5L)), class = "data.frame", row.names = c(NA, -2L), .Names = c("UserID", "Q3_1", "Q3_2", "Q3_3", "Q3_4"))
#what I'd like to see, but doesn't work
test %>% mutate(total = sum(starts_with("Q3"), na.rm = TRUE))
#What I'd like to end up with:
UserID Q3_1 Q3_2 Q3_3 Q3_4 total
1 53017366 3 2 3 NA 8
2 53017366 3 2 3 5 13
One option suggested here and here requires putting an entire new select column in the data and using a rowwise function (like rowSums).