简体   繁体   中英

Extract top_n variables name for each group

For each group, I'm trying to get the top_n car_names to appear in a new column comma separated.

For example, when you run the code below you'll see the top 2 mpg cars per group (cyl). Next, I want to extract the top two cars (or more, if there is a tie) and store them together into a new column called car_summary.

mtcars2 %>% 
  select(mpg, cyl, car_name) %>% 
  group_by(cyl) %>%  
  mutate(Score = rank(mpg, ties.method = "max")) %>%
  arrange(desc(Score)) %>% top_n(2,Score)

The expected output looks like below

cyl  <-  c(8,4,6)
car_summary <-  c("Pontiac Firebird, Hornet Sportabout", "Toyota Corolla, 
Fiat 128", "Hornet 4 Drive, Mazda RX4, Mazda RX4 Wag")

data.frame(cyl, car_summary)

  cyl                                  car_summary
1   8          Pontiac Firebird, Hornet Sportabout
2   4                     Toyota Corolla, Fiat 128
3   6     Hornet 4 Drive, Mazda RX4, Mazda RX4 Wag

You need toString from base R -

mtcars2 %>% 
  select(mpg, cyl, car_name) %>% 
  group_by(cyl) %>%  
  mutate(Score = rank(mpg, ties.method = "max")) %>%
  arrange(desc(Score)) %>%
  top_n(2,Score) %>%
  summarize(car_summary = toString(car_name))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM