added 171 characters in body
Source Link
Oxbowerce
  • 4.9k
  • 2
  • 6
  • 18

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

library(dplyr)

df_n <- data.frame(
  id = c(1, 2, 3),
  lemma = c("word1", "word2", "word3"),
  year = c(1970, 1971, 1972), 
  count = c(737, 767, 988)
)

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)

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)

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

library(dplyr)

df_n <- data.frame(
  id = c(1, 2, 3),
  lemma = c("word1", "word2", "word3"),
  year = c(1970, 1971, 1972), 
  count = c(737, 767, 988)
)

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)
Source Link
Oxbowerce
  • 4.9k
  • 2
  • 6
  • 18

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)