为每个组提取top_n变量名称

时间:2018-11-13 04:44:58

标签: r dplyr

对于每个组,我试图将top_n car_names出现在以逗号分隔的新列中。

例如,当您运行下面的代码时,您将看到每组(cyl)排名前2的mpg汽车。接下来,我要提取前两辆车(如果有平局,则更多),并将它们一起存储到名为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)

预期输出如下所示

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

1 个答案:

答案 0 :(得分:0)

您需要{R为基数的toString-

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))