R - Get number of values per group without counting NAs

烈酒焚心 提交于 2019-12-11 10:54:23

问题


So I'm trying to count the number of values per group in a column without counting the NAs. I've tried doing it with "length" but I can't figure out how to tell "length" to leave the NAs be, when in the context of looking at values per group.

I've found similar problems but couldn't figure out how to apply the solutions to my case:

Length of columns excluding NA in r

http://r.789695.n4.nabble.com/Length-of-vector-without-NA-s-td2552208.html

I've created a minimal working example to illustrate the problem:

# 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  

I'd appreciate any kind of help!

Thx, Carina


回答1:


If we are using real NAs without quoting, we can use is.na and table to find the count.

table(!is.na(value), group)[2,]
#A B C 
#2 2 1 

data

value <- c(3,10,9,NA,5,NA,NA,4)
group <- c("A","A","B","C","B","A","A","C")



回答2:


... or using functions filter and count from package dplyr:

library(dplyr)
example %>%
    filter(!is.na(value)) %>%
    count(group)

PS: as akrun mentioned, specify NA in your vector without quotes. otherwise value will be casted to an character-vector c("3","10","9","NA",...)




回答3:


There are probably more elegant ways to solve, but one way would be to use an anonymous function remove NA's before taking the length.

tapply(example$value, example$group, function(x) {length(x[!is.na(x)])})

By the way, you enclosed your NA in quotes in the example. This will cause R to see "NA" as a string and not a missing value. And you won't get your expected value with the right solution. I believe the example you are looking for is

value <- c(3,10,9,NA,5,NA,NA,4)


来源:https://stackoverflow.com/questions/32251996/r-get-number-of-values-per-group-without-counting-nas

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!