MRJJ17
MRJJ17

Reputation: 117

Count number of weekly events per artist in R data.table

My goal is to create a data.table which shows the total number of events per week per artist. Furthermore, I would also like to add two columns which specify the number of events and the number of concerts given per artist in a particular week.

My data.table looks like this:

 Year_week  artist_id   type    event_id
 17\2       464836     Concert  54446
 17\2       4234234    Festival 6654
 17\3       89543      Concert  5321
 17\3       55211      Concert  85642
 17\4       32167      Concert  43222
 17\4       9876       Concert  13131

I already tried to aggregate by week for the total number of events. However, as I am quite new to R I have the feeling what I am doing is not the right way.

#number of events per week per artist
 USevents_weekly_total = USevents[, list(number_of_events = .N (unique(event_id))),
                                      by=c('Year_week', 'artist_id')]

What would be the most convenient way to do this in data.table?

Upvotes: 3

Views: 122

Answers (1)

akrun
akrun

Reputation: 886938

Assuming that we need to find the number of unique elements in 'event_id' for 'Year_week', 'artist_id', and 'Artist', the uniqueN function can be used

USevents_weekly_total <- USevents[, list(number_of_events =  uniqueN(event_id)),
                                  by=c('Year_week', 'artist_id', 'Artist')]

Or as @Frank mentioned

unique(USevents[, .(Year_week, artist_id, Artist)])[, .N, by=.(Year_week, artist_id)]

Upvotes: 3

Related Questions