[英]Counting number of observations per column and by group
I have the following data - here is a snapshot, there are many more score and weight columns.我有以下数据 - 这是一个快照,还有更多的分数和权重列。
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))
I would like to count for each score column, the number of observations by class.我想为每个得分列计算 class 的观察次数。 For example,
例如,
Class score_Jan_2018 score_Feb_2018
1 3 3
2 1 2
3 4 4
I tried the following but it only produced a series of NA for most values.我尝试了以下方法,但它只为大多数值产生了一系列 NA。
flows_by_class = flows %>%
group_by(Class) %>%
summarise_at(vars(starts_with("score_")), ~sum(.!= 0))
Any help is appreciated.任何帮助表示赞赏。
Using by()
in base R.在基础 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.