【发布时间】:2015-08-27 14:22:43
【问题描述】:
所以我试图在不计算 NA 的情况下计算列中每组的值数。 我试过用“长度”来做,但在查看每组值的情况下,我不知道如何告诉“长度”让 NA 保持不变。
我发现了类似的问题,但不知道如何将解决方案应用于我的案例:
Length of columns excluding NA in r
http://r.789695.n4.nabble.com/Length-of-vector-without-NA-s-td2552208.html
我创建了一个最小的工作示例来说明问题:
# making some data
value <- c(3,10,9,"NA",5,"NA","NA",4)
group <- c("A","A","B","C","B","A","A","C")
example <- data.frame(value, group)
example
# value group
# 1 3 A
# 2 10 A
# 3 9 B
# 4 NA C
# 5 5 B
# 6 NA A
# 7 NA A
# 8 4 C
# trying to extract the number of values (without counting NAs) for each group
n.example <- tapply(example$value, list(example$group), length)
n.example
# A B C
# 4 2 2
#Correct answer would be:
# A B C
# 2 2 1
如果能提供任何帮助,我将不胜感激!
谢谢, 船底座
【问题讨论】: