使用列和行中的多个值整理数据帧

时间:2016-09-09 10:25:52

标签: r dplyr tidyr

我从网站上删除了几张用于查找表的表格。我最终得到了类似的东西:

a<-tibble(Group=c("A","B","B","B","C","C"),Code=c("2,3","15,16,18,","11,12,13,19,","14,13,15,18,110","111,","178,152,111"))

现在我想把它转换成一个长的tibble,其中两列在Group和d中具有唯一的键值对。我设法做到了,但我的解决方案似乎相当冗长。有没有另一种方法可以将单独和聚集结合起来实现自动化?

a2<-a %>% separate(Code,sep = ',',into = paste0("n",c(1:5)),fill = "right",convert = T) %>% 
  gather(key=n,value = d,-Group) %>% 
  select(-n) %>% 
  filter(d!="" & !is.na(d)) %>%
  distinct() %>% 
  arrange(Group,d)

0 个答案:

没有答案