Four solutions:
Base R frame:
aggregate(DF$Prop, by=list(Fam=DF$Fam), FUN=function(a) sum(!is.na(a)))
# Fam x
# 1 A 5
# 2 B 6
# 3 C 4
Base R, "table" (which is not a frame, see as.data.frame(xtabs(...))
to see the frame variant ... a little different):
xtabs(~ Fam + is.na(Prop), data=DF)
# is.na(Prop)
# Fam FALSE TRUE
# A 5 1
# B 6 1
# C 4 3
dplyr
:
library(dplyr)
DF %>%
group_by(Fam) %>%
summarize(n = sum(!is.na(Prop)))
# # A tibble: 3 x 2
# Fam n
# <fct> <int>
# 1 A 5
# 2 B 6
# 3 C 4
data.table
library(data.table)
# data.table 1.11.4 Latest news: http://r-datatable.com
# Attaching package: 'data.table'
# The following objects are masked from 'package:dplyr':
# between, first, last
DT <- as.data.table(DF)
DT[,sum(!is.na(Prop)),keyby=.(Fam)]
# Fam V1
# 1: A 5
# 2: B 6
# 3: C 4
Data:
DF <- data.frame(Fam=sample(c('A','B','C'), size=20, replace=TRUE), Prop=sample(c('one','two','three'), size=20, replace=TRUE))
DF$Prop[sample(20,size=5)] <- NA
DF
# Fam Prop
# 1 B one
# 2 B three
# 3 C <NA>
# 4 A <NA>
# 5 C one
# 6 A two
# 7 B one
# 8 A three
# 9 B two
# 10 C one
# 11 C two
# 12 B three
# 13 C <NA>
# 14 C <NA>
# 15 A one
# 16 A one
# 17 B three
# 18 A two
# 19 C two
# 20 B <NA>