1 of 2
Oxbowerce
  • 5.6k
  • 2
  • 6
  • 20

This can be done using dplyr and a combination of group_by and aggregations functions. Something like this should work:

library(dplyr)

df_n %>%
    # create column that specifies the decade
    mutate(decade = year - year %% 10) %>%
    group_by(decade, lemma) %>%
    # add up counts for duplicate words within groups specified above
    summarise(count = sum(count)) %>%
    group_by(decade) %>%
    # select top 10 records based on count within groups specified above
    top_n(10, count)
Oxbowerce
  • 5.6k
  • 2
  • 6
  • 20