0

I have data like this:

ID <- c(1001,1001, 1001, 1002,1002,1002)
activity <- c(123,123,123, 456,456,789)
df<- data.frame(ID,activity)

I want to count the number of unique activity values within ID to end up with a dataframe like this:

N<- c(1,1,1,2,2,2)
data.frame(df,N)

So we can see that person 1001 did only 1 activity while person 1002 did two.

I think it can be done with aggregate but am happy to use another approach.

Log On
  • 69
  • 9

1 Answers1

1

dplyr option

sum_df <- df %>%
  group_by(ID) %>%
  summarize(count_distinct = n_distinct(activity)) %>%
  left_join(df,
          by = 'ID')

Harry Smith
  • 267
  • 1
  • 11