【问题标题】:How to find sum of certain rows in R to get a grand total per row?如何在 R 中找到某些行的总和以获得每行的总计?
【发布时间】: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

【问题讨论】:

    标签: r dplyr group-by rowsum


    【解决方案1】:

    这是一个分组数据,使用cur_data()select,否则分组变量也会作为属性出现,从而导致错误

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

    -输出

    # 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
    

    【讨论】:

    • 谢谢!但我期待每个人的总和都是 8(例如,贾斯汀出现两次,等等)我们如何让它看起来像这样?这就是我想将它们组合在一起的原因。
    • @J.Sabree 你只是在做rowSums。为此,您需要这样做sum(rowSums
    • 谢谢!我不知道你可以把它们叠起来。所以不会让我再接受 5 分钟的答案,但我会这样做。谢谢!
    猜你喜欢
    • 2019-12-28
    • 2015-09-16
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多