1

I am fairly new to R and I currently trying out dplyr with the babynames dataset. So far everything is working perfectly fine, however I cant think of any function/code that would extract me the most popular name for each year from the dataset.

If anybody could show me such a function or point me in the right direction I would be very grateful!

Thank you very much!

nhaus
  • 786
  • 3
  • 13
  • 1
    can you provide a portion of the dataset you are using ? and also the code you have try so far ? (please check how to add a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)) – dc37 Nov 19 '19 at 08:28
  • Apparently, `babynames` is a [CRAN package](https://cran.r-project.org/web/packages/babynames/). – Rui Barradas Nov 19 '19 at 08:33

1 Answers1

3

If you want the most popular name regardless of the sex, then you can try this:

library(dplyr)
most_popular <- babynames %>% 
    group_by(year) %>% 
    top_n(1, wt=n)
Valeri Voev
  • 1,982
  • 9
  • 25
  • `library(dplyr) library(babynames) male_tops <- babynames %>% filter(sex=="M") %>% group_by(year) %>% top_n(1, wt=n) female_tops <- babynames %>% filter(sex=="F") %>% group_by(year) %>% top_n(1, wt=n) total <- rbind(male_tops, female_tops)` this is what i have so far. Is there a way by which i can combine two dataframes a row wise, and not the whole dataframe at once? – nhaus Nov 19 '19 at 09:49
  • I am not quite sure what you mean, but you can achieve the same result a bit more succinctly like this: `library(dplyr) library(babynames) library(tidyr) most_popular <- babynames %>% group_by(year, sex) %>% top_n(1, wt=n)` and if you would like a more "wide" format like this: `library(dplyr) library(babynames) library(tidyr) most_popular <- babynames %>% group_by(year, sex) %>% top_n(1, wt=n) %>% pivot_wider(names_from = c(sex), values_from = c(n, name, prop))` – Valeri Voev Nov 20 '19 at 13:28