RoyalTS
RoyalTS

Reputation: 10203

Count distinct over multiple columns in data.table

I observe users at different times and in different situations 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-situations 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

Answers (1)

user7982431
user7982431

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

Related Questions