我试着用dplyr来包装我对重组业务的了解,但是解决不了这个问题,也许你们中的一个能帮上忙:)
df <- data.frame(
gene = c("ABC", "ABC", "AA", "AB", "AC", "DD", "DE", "AA", "AR", "ABC"),
genotype = c("ht", "cpht", "ht", "cpht", "hm", "hm", "cpht", "ht", "hm", "cpht"),
consequence = c("utr3", "miss", "miss", "stop", "utr5", "miss", "stop", "miss", "utr3", "utr5")
)我想创建一个新的df,如下所示:

据说这应该很容易用dplyr完成,但我不能让它开始工作。也许你们中的一个可以?
非常感谢!塞巴斯蒂安
发布于 2018-10-29 09:53:04
你可以试试这个:
df %>%
group_by(gene,genotype) %>%
summarise(consequence=paste(consequence,collapse=",")) %>%
spread(genotype,consequence)
## A tibble: 7 x 4
## Groups: gene [7]
# gene cpht hm ht
# <fct> <chr> <chr> <chr>
#1 AA <NA> <NA> miss,miss
#2 AB stop <NA> <NA>
#3 ABC miss,utr5 <NA> utr3
#4 AC <NA> utr5 <NA>
#5 AR <NA> utr3 <NA>
#6 DD <NA> miss <NA>
#7 DE stop <NA> <NA>如您在文章中所提供的,您的数据如下:
df <- data.frame(
gene = c("ABC", "ABC", "AA", "AB", "AC", "DD", "DE", "AA", "AR", "ABC"),
genotype = c("ht", "cpht", "ht", "cpht", "hm", "hm", "cpht", "ht", "hm", "cpht"),
consequence = c("utr3", "miss", "miss", "stop", "utr5", "miss", "stop", "miss", "utr3", "utr5")
)
df
# gene genotype consequence
#1 ABC ht utr3
#2 ABC cpht miss
#3 AA ht miss
#4 AB cpht stop
#5 AC hm utr5
#6 DD hm miss
#7 DE cpht stop
#8 AA ht miss
#9 AR hm utr3
#10 ABC cpht utr5https://stackoverflow.com/questions/53042707
复制相似问题