1

I have a dataset that has employees' capacity each month, and I want to get a total for each employee across all months:

library(dplyr)
data <- tibble(employee = c("Justin", "Corey","Sibley", "Justin", "Corey","Sibley"),
               education = c("graudate", "student", "student", "graudate", "student", "student"),
               fte_max_capacity = c(1, 2, 3, 1, 2, 3),
               project = c("big", "medium", "small", "medium", "small", "small"),
               aug_2021 = c(1, 1, 1, 1, 1, 1),
               sep_2021 = c(1, 1, 1, 1, 1, 1),
               oct_2021 = c(1, 1, 1, 1, 1, 1),
               nov_2021 = c(1, 1, 1, 1, 1, 1))

I've tried following using the code found here, but I get this error:

data %>%
  dplyr::select(-contains("project")) %>%
  dplyr::group_by(employee) %>%
  mutate(sum = rowSums(select(., vars(contains("_20")))))

Error: Problem with `mutate()` input `sum`.
x Must subset columns with a valid subscript vector.
x Subscript has the wrong type `quosures`.
ℹ It must be numeric or character.
ℹ Input `sum` is `rowSums(select(., vars(contains("_20"))))`.
ℹ The error occurred in group 1: employee = "Corey".

I also tried this a modified version of the solution from this website. But I also get an error, despite all the relevant columns being numeric:

data %>%
  dplyr::select(-contains("project")) %>%
  dplyr::group_by(employee) %>%
  mutate_at(vars(contains("_20"), rowSums(., na.rm = T)))

Error: 'x' must be numeric

J.Sabree
  • 2,280
  • 19
  • 48

1 Answers1

1

It is a grouped data, use cur_data() to do the select otherwise, the grouped variable will also be present as attribute and thus cause the error

library(dplyr)
data %>%
  dplyr::select(-contains("project")) %>%
  dplyr::group_by(employee) %>%
  dplyr::mutate(sum = sum(rowSums(select(cur_data(), contains("_20"))))) %>%
  ungroup

-ouptut

# A tibble: 6 x 8
  employee education fte_max_capacity aug_2021 sep_2021 oct_2021 nov_2021   sum
  <chr>    <chr>                <dbl>    <dbl>    <dbl>    <dbl>    <dbl> <dbl>
1 Justin   graudate                 1        1        1        1        1     8
2 Corey    student                  2        1        1        1        1     8
3 Sibley   student                  3        1        1        1        1     8
4 Justin   graudate                 1        1        1        1        1     8
5 Corey    student                  2        1        1        1        1     8
6 Sibley   student                  3        1        1        1        1     8
akrun
  • 874,273
  • 37
  • 540
  • 662
  • thank you! But I was expecting that everyone's sum would be 8 (e.g., Justin appears twice, etc.) How would we get it to appear like that? That's why I wanted to group them together. – J.Sabree Aug 03 '21 at 21:50
  • 1
    @J.Sabree you are only doing `rowSums`. For that you need to do `sum(rowSums` – akrun Aug 03 '21 at 21:51
  • 1
    thank you! I didn't know you could stack them. SO won't let me accept the answer for 5 more minutes, but I'll do so then. Thanks! – J.Sabree Aug 03 '21 at 21:54