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