当我有如下数据集时:
df <- structure(list(Sample = c("abc", "abc", "def", "ghi", "jkl",
"abc", "mno", "def", "def"), Types = c("A", "A", "A", "A", "B",
"B", "C", "C", "C"), values = c("x", "y", "x", "x", "z", "x",
"y", "z", "x")), class = "data.frame", row.names = c(NA, -9L))
我想使用pivot_wide:
使数据集如下所示Sample A B C
<chr> <chr> <chr> <chr>
1 abc x;y x
2 def x z;x
3 ghi x
4 jkl z
5 mno y
我试着运行代码:
a<- mut_simple %>%
pivot_wider(names_from = Types, values_from = values)
但是有一些关于重复的警告消息(例如abc-A, def-C)。
如何使用;
您可以在values_fn
-
df <- tidyr::pivot_wider(df, names_from = Types, values_from = values,
values_fn = function(x) paste0(x, collapse = ';'))
df
# Sample A B C
# <chr> <chr> <chr> <chr>
#1 abc x;y x NA
#2 def x NA z;x
#3 ghi x NA NA
#4 jkl NA z NA
#5 mno NA NA y
同样,在data.table
-
library(data.table)
dcast(setDT(df), Sample~Types, value.var = 'values',
fun.aggregate = function(x) paste0(x, collapse = ';'))
df <- structure(list(Sample = c("abc", "abc", "def", "ghi", "jkl",
"abc", "mno", "def", "def"), Types = c("A", "A", "A", "A", "B",
"B", "C", "C", "C"), values = c("x", "y", "x", "x", "z", "x",
"y", "z", "x")), class = "data.frame", row.names = c(NA, -9L))