【问题标题】:Is there an R function that can aggregate the count of a specific row in a categorical column?是否有一个 R 函数可以聚合分类列中特定行的计数?
【发布时间】:2022-01-08 19:36:06
【问题描述】:

我希望每个人都做得很好。我在尝试在 R 中聚合时有点放屁。假设我有这个 df:

student subject
Amber math
Colin math
Bob science
Amber math
Amber science

我想计算学生的科目是数学的次数并将其添加到数据框中,因此结果如下所示:

student subject total 'math'
Amber math 2
Colin math 1
Bob science 0
Amber math 2
Amber science 2

这可能吗?我尝试了 aggregate(subject["math"] ~ student, data = df, length) 只是为了完成第一部分,但我得到“model.frame.default 中的错误(formula = subject["math"] ~ : variable长度不同(为“学生”找到)”。

提前谢谢你!

【问题讨论】:

  • 你想要的是 ave 而不是 aggregate

标签: r dataframe count aggregate


【解决方案1】:

我认为你想要这样的东西

library(magrittr)
library(dplyr)

df <- data.frame(
   student = c("Amber", "Colin", "Bob", "Amber", "Amber"),
   subject = c("math", "math", "science", "math", "science")
)

df %>% group_by(student,subject) %>% mutate(`Total math` = n()) %>% filter(`Total math` > 0) %>% filter (subject=="math") %>% distinct -> df2

merge(x=df, y=df2, by="student", all.x = TRUE) %>% mutate(`Total math` = ifelse(!is.na(`Total math`), `Total math`,0)) %>% rename(subject="subject.x") %>% select(student, subject, `Total math`) %>% print

【讨论】:

    【解决方案2】:

    我尝试了一种不同的方法,它与您的期望输出不同,但这对您有用吗?

    my_df <- data.frame("Student" = c("Amber", "Colin", "Bob", "Amber", "Amber"),
                    "Subject" = c("math", "math", "science", "math", "science"),
                    stringsAsFactors = FALSE)
    
    my_df <- my_df %>% group_by(Student, Subject) %>% summarise("Total" = n())
    

    【讨论】:

      【解决方案3】:
      library(dplyr)
      df_with_count<-df%>%group_by(student,subject)%>%mutate(count=n())
      

      在这里找到: https://www.tutorialspoint.com/how-to-add-a-new-column-in-an-r-data-frame-with-count-based-on-factor-column

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-08
        • 1970-01-01
        • 2011-12-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多