按列和组对观察值进行计数

时间:2019-09-23 05:52:34

标签: r

我有以下数据-这是快照,还有更多得分和权重列。

    flows <- structure(list(Student = c("Adam", "Char", "Fred", "Greg", "Ed", "Mick", "Dave", "Nick", "Tim", "George", "Tom"), 
                            Class = c(1L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), score_Jan_2018 = c(NA, 5L, -7L, 2L, 1L, NA, 5L, 8L, -2L, 5L, NA), 
                            score_Feb_2018 = c(2L,   0, 8L, NA, 2L, 6L, NA, 8L, 7L, 3L, 8L), Weight_Jan_2018 = c(150L, 30L, NA, 80L, 60L, 80L, 40L, 12L, 23L, 65L, 78L), 
                            Weight_Feb_2018 = c(153L, 60L, 80L, 40L, 80L, 30L, 25L, 45L, 40L, NA, 50L)), class = "data.frame", row.names = c(NA, -11L))

我想为每个分数列计算按班级的观察次数。例如,

  Class   score_Jan_2018  score_Feb_2018
    1          3              3
    2          1              2
    3          4              4

我尝试了以下操作,但是对于大多数值它只产生了一系列NA。

flows_by_class = flows %>%  
  group_by(Class) %>%
  summarise_at(vars(starts_with("score_")), ~sum(.!= 0))

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

在底数R中使用by()

cbind(Class=unique(flows$Class), sapply(names(flows)[3:4], function(x)
  by(flows[x], flows$Class, function(x) length(na.omit(x[x != 0])))))
#   Class score_Jan_2018 score_Feb_2018
# 1     1              3              2
# 2     2              1              2
# 3     3              4              4