Lacking a reproducible example, here's some sample data:
set.seed(42)
df <- data.frame(patient = sample(c(123,456), size=30, replace=TRUE), encounter=sample(c(12,34,56,78,90), size=30, replace=TRUE))
head(df)
# patient encounter
# 1 456 78
# 2 456 90
# 3 123 34
# 4 456 78
# 5 456 12
# 6 456 90
Base R:
aggregate(x = df$encounter, by = list(patient = df$patient),
FUN = function(a) length(unique(a)))
# patient x
# 1 123 5
# 2 456 5
or (by @20100721's suggestion):
aggregate(encounter~.,FUN = function(t) length(unique(t)),data = df)
Using dplyr
:
library(dplyr)
group_by(df, patient) %>%
summarize(numencounters = length(unique(encounter)))
# # A tibble: 2 x 2
# patient numencounters
# <dbl> <int>
# 1 123 5
# 2 456 5
Update: @2100721 informed me of n_distinct
, effectively same as length(unique(...))
:
group_by(df, patient) %>%
summarize(numencounters = n_distinct(encounter))