【发布时间】:2021-08-03 21:42:52
【问题描述】:
我有一个包含每个月员工容量的数据集,我想获得每个员工在所有月份的总数:
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))
我已尝试使用找到的代码 here 进行跟踪,但出现此错误:
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".
我还尝试了此website 中解决方案的修改版本。但我也得到一个错误,尽管所有相关的列都是数字:
data %>%
dplyr::select(-contains("project")) %>%
dplyr::group_by(employee) %>%
mutate_at(vars(contains("_20"), rowSums(., na.rm = T)))
Error: 'x' must be numeric
【问题讨论】: