Reputation: 10203
I observe user
s at different time
s and in different situation
s and potentially I see them multiple times, like so:
df <- data.table(time = c(1,1,1,2,2),
user = c(1,1,2,1,2),
situation = c(1,1,1,2,2),
observation = c(1,2,1,1,1))
What I would like to do is to count the number of user
-situation
s in each time period using data.table
. Expected output:
result <- data.table(time = c(1,2),
user_situations = c(2,2))
I know I can do this in a chained way:
unique(df[, .(time, user, situation)])[, .(user_situations = .N), .(time)]
but wonder if there's a simple way to do this in one go.
Upvotes: 5
Views: 5785
Reputation: 69
dplyr
solution:
library(dplyr)
df <- data.table(time = c(1,1,1,2,2),
user = c(1,1,2,1,2),
situation = c(1,1,1,2,2),
observation = c(1,2,1,1,1))
df %>% group_by(time) %>%
distinct(user, situation) %>%
summarise(user_situations = n())
# tbl_dt [2 × 2]
time user_situation
<dbl> <int>
1 1 2
2 2 2
Upvotes: 6