I would like to perform rowwise operations on an arbitrary set of columns using column names instead of index numbers. I know this is possible using base r and columns indexed by numbers, but it would be less error prone for me if I could do this using column names within a tidyverse pipe.
Here's the basis for what I am trying to do.
library(tidyverse)
df <- tibble(name = c("Sam", "Jane", "Erin", "Bert", "Lola"),
age = c(11, 10, 11, 12, 10),
score_a = c(19, 16, 16, 5, 10),
score_b = c(10, 10, 10, 10, 10))
df %>%
rowwise() %>%
mutate_at(vars(score_a:score_b),funs(total = sum))
#> Source: local data frame [5 x 6]
#> Groups: <by row>
#>
#> # A tibble: 5 x 6
#> name age score_a score_b score_a_total score_b_total
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 Sam 11 19 10 19 10
#> 2 Jane 10 16 10 16 10
#> 3 Erin 11 16 10 16 10
#> 4 Bert 12 5 10 5 10
#> 5 Lola 10 10 10 10 10
Created on 2019-01-10 by the reprex package (v0.2.1)
I would actually like a table that looks like this
#> # A tibble: 5 x 6
#> name age score_a score_b total
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 Sam 11 19 10 29
#> 2 Jane 10 16 10 27
#> 3 Erin 11 16 10 26
#> 4 Bert 12 5 10 15
#> 5 Lola 10 10 10 20
Created on 2019-01-10 by the reprex package (v0.2.1)