user18758152
user18758152

Reputation: 1

How to aggregate (sum and count) in R using dplr and remove rows on a condition?

I have a dataset called songs.merge with variables songID, songName, year, artist, score1, score2 and score.diff

I want to aggregate(sum) the score.diff according to the artist, count the number of songs per artist, and then remove any artists with fewer than 4 songs in the dataframe.

I am trying to use dplyr in r but it is not working. How should I proceed?

songs.merge %>% 
   group_by(artist) %>% 
      summarise_at(vars(diff),funs(sum(.,na.rm=TRUE)),
                   vars(songID), funs(count()))

Upvotes: 0

Views: 222

Answers (1)

user18309711
user18309711

Reputation:

suggestion:

songs.merge %>% 
   group_by(artist) %>% 
   summarise(sum_diff = sum(score.diff, na.rm = TRUE),
             song_count = n()) %>%
   filter(song_count > 3)

Upvotes: 1

Related Questions