如何基于另一个小标题的信息添加小标题列

时间:2019-11-21 21:02:06

标签: r tibble

我有以下两个小标题:

my_stats_tbl <- tribble(
  ~year,   ~model_id,
  2011,      "132",
  2012,      "145,167",
  2013,      "132,145,167",
  2014,      "132,174",
  2015,      "174,182,183",
  2016,      "183",
  2017,      "191"
)

model_tbl <- tribble(
  ~id,      ~name,
  132,      "Race",
  145,      "Out",
  167,      "Lazer",
  174,      "Wow",
  182,      "Super",
  183,      "Tornado",
  191,      "Cloud"
)

我想在第一列中添加另一列,以便可以基于第二个小标题中的值列出模型名称。这是预期的输出应如下所示:

my_new_stats_tbl <- tribble(
  ~year,   ~model_id,            ~model_name,
  2011,      "132",                "Race",
  2012,      "145,167",            "Out,Lazer",
  2013,      "132,145,167",        "Race,Out,Lazer",
  2014,      "132,174",            "Race,Wow",
  2015,      "174,182,183",        "Wow,Super,Tornado",
  2016,      "183",                "Tornado",
  2017,      "191",                "Cloud"
)

我该如何实现?

谢谢

1 个答案:

答案 0 :(得分:2)

以下是根据Camille的评论提出的解决方案:

library(tidyr)
library(dplyr)
#make tidy
newstats<-my_stats_tbl %>% separate_rows(model_id, sep=",") 
#convert id from character to number
newstats$model_id<-as.integer(newstats$model_id)
#join tables
newstats<-left_join(newstats, model_tbl, by=c("model_id" = "id"))
#make untidy
newstats %>% group_by(year) %>%  summarize(model_id = paste0(model_id, collapse = ","), name=paste0(name, collapse = ",")) 

# # A tibble: 7 x 3
#   year model_id    name             
#   <dbl> <chr>       <chr>            
# 1  2011 132         Race             
# 2  2012 145,167     Out,Lazer        
# 3  2013 132,145,167 Race,Out,Lazer   
# 4  2014 132,174     Race,Wow         
# 5  2015 174,182,183 Wow,Super,Tornado
# 6  2016 183         Tornado          
# 7  2017 191         Cloud