【问题标题】:How do I reduce this data frame by groups?如何按组减少此数据框?
【发布时间】:2017-03-25 18:31:04
【问题描述】:

我有以下

t <- structure(list(name = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("Alice", "Bob",
"Jane Doe", "John Doe"), class = "factor"), school = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("Alice School",
"Bob School", "Someother School", "Someschool College"), class = "factor"),
    group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L,
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
    1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("A", "B"), class = "factor"),
    question = structure(c(2L, 4L, 6L, 8L, 1L, 3L, 5L, 7L, 2L,
    4L, 6L, 8L, 1L, 3L, 5L, 7L, 2L, 4L, 6L, 8L, 1L, 3L, 5L, 7L,
    2L, 4L, 6L, 8L, 1L, 3L, 5L, 7L), .Label = c("q1", "q2", "q3",
    "q4", "q5", "q6", "q7", "q8"), class = "factor"), mark = c(0L,
    0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L,
    1L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 1L,
    1L), subject = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
    1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L,
    2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("C", "M"), class = "factor")), .Names = c("name",
"school", "group", "question", "mark", "subject"), row.names = c(7L,
15L, 23L, 31L, 3L, 11L, 19L, 27L, 8L, 16L, 24L, 32L, 4L, 12L,
20L, 28L, 6L, 14L, 22L, 30L, 2L, 10L, 18L, 26L, 5L, 13L, 21L,
29L, 1L, 9L, 17L, 25L), class = "data.frame")

我需要生成一个数据框,其中每个学生的每个科目都有一个组合分数。该组合只是每个问题上的分数的总和。因此,例如,Jane Doe 将有 3 个主题 C 和 2 个主题 M。我已经用 Reduce 和其他方法敲了足够长的时间。我可能会以一种非常程序化的方式解决这个问题,但如果我能用单线(或近似值)来解决这个问题,我会更开心。我相信它可以做到......

【问题讨论】:

    标签: r reduce


    【解决方案1】:

    您在问题中说过;你想group_by学生和学科并计算总和

    library(tidyverse)
    asdf %>%
      group_by(name, subject) %>%
      summarise(score = sum(mark))
    

    【讨论】:

      【解决方案2】:

      这里是data.table 解决方案:

      library(data.table)
      setDT(t)[, sum(mark), by = list(name, subject)]
      

      【讨论】:

        【解决方案3】:

        为了完整起见,基础 R:

        aggregate(mark ~ name + subject, data=t, sum)
        

        这表示“使用sum 作为聚合函数,通过分组变量namesubject 聚合响应变量mark”。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-22
          • 2011-10-13
          • 1970-01-01
          • 1970-01-01
          • 2022-06-29
          • 2019-09-21
          相关资源
          最近更新 更多