我们可能需要使用separate_rows
library(tidyverse)
df1 %>%
separate_rows(V1, sep=",")
如果我们想得到matrix的输出
df1 %>%
separate(V1, into = c("V1", "V2"), sep=",") %>%
spread(V2, n, fill = 0) %>%
column_to_rownames("V1")
# Chris James Jones Otto Peter
#David 0 0 6 0 0
#Harry 0 0 0 8 0
#Jeff 0 5 0 0 0
#@Sam 30 0 0 0 81
通过在行名和列名中包含名字和姓氏,可以将其转换为方阵
tmp <- df1 %>%
separate(V1, into = c("V1", "V2"), sep=",")
lvls <- sort(unique(unlist(tmp[1:2])))
tmp %>%
mutate_at(vars(V1, V2), factor, levels = lvls) %>%
spread(V2, n, fill = 0, drop = FALSE)
数据
df1 <- structure(list(V1 = c("Sam,Chris", "Sam,Peter", "Jeff,James",
"David,Jones", "Harry,Otto"), n = c(30L, 81L, 5L, 6L, 8L)),
class = "data.frame", row.names = c("1",
"2", "3", "4", "5"))