[英]Split a long form into rows but with duplicates merged as a string delimited with “;”?
Suppose I have a dataframe that I want to split into rows.假设我有一个要拆分成行的数据框。
temp = data.frame ( group=c('a','b','c'), fruits = c('apple', 'orange', 'none'), days=c('mon','tues','wed') )
reshape2::dcast(temp , days ~ group, value.var=c ( "fruits") )
days a b c
1 mon apple <NA> <NA>
2 tues <NA> orange <NA>
3 wed <NA> <NA> none
This works fine, however when I add a duplicated row as such.这工作正常,但是当我添加重复的行时。
temp = rbind ( temp, c('a','orange','mon') )
the casting would failed and only show the total.铸造失败,只显示总数。 What I really want is something like this.我真正想要的是这样的东西。
days a b c
1 mon apple;orange <NA> <NA>
2 tues <NA> orange <NA>
3 wed <NA> <NA> none
thanks!谢谢!
Try this tidyverse
solution.试试这个tidyverse
解决方案。 You can aggregate your data using paste0()
to get a proper structure in order to transform to wide format:您可以使用paste0()
聚合您的数据以获得适当的结构,以便转换为宽格式:
library(tidyverse)
#Code
temp %>%
group_by(group,days) %>%
summarise(fruits=paste0(fruits,collapse = ';')) %>%
pivot_wider(names_from = group,values_from=fruits)
Output:输出:
# A tibble: 3 x 4
days a b c
<chr> <chr> <chr> <chr>
1 mon apple;orange NA NA
2 tues NA orange NA
3 wed NA NA none
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.