有一个调查数据集。 2个问题有多项选择选项,不允许我使用
pivot_wider
这是示例数据。
names=c("per_id","quest","answ")
per_id=c("0012","0023","0045","0003","0003","0045","0003","0012","0023","0004","0004","0001","0005","0546","0001","0546","0005","0004","0004")
quest=c("A","A","A","A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B")
answ=c("Self","Self","Self","Self","Father","Sister","Daughter","Mother","Father","Self","Father","X1","Y1","Z1","Y1","Z2","X1","X1","X1")
df=cbind.data.frame(per_id,quest,answ)
names(df)=names
我想要拥有的是
你可以尝试一下
l = lapply(split(df, df$quest), \(x) {x$quest = NULL; x})
names(l$A)[2] = "A"
names(l$B)[2] = "B"
Reduce(\(...) merge(..., all=TRUE), l)
这给出了
per_id A B
1 0001 <NA> X1
2 0001 <NA> Y1
3 0003 Self <NA>
4 0003 Father <NA>
5 0003 Daughter <NA>
6 0004 Self X1
7 0004 Self X1
8 0004 Father X1
9 0004 Father X1
10 0005 <NA> Y1
11 0005 <NA> X1
12 0012 Self <NA>
13 0012 Mother <NA>
14 0023 Self <NA>
15 0023 Father <NA>
16 0045 Self <NA>
17 0045 Sister <NA>
18 0546 <NA> Z1
19 0546 <NA> Z2
要删除重复的行,请执行以下操作
x = Reduce(\(...) merge(..., all=TRUE), l)
x[!duplicated(x), ]
数据:
df = structure(list(per_id = c("0012", "0023", "0045", "0003", "0003",
"0045", "0003", "0012", "0023", "0004", "0004", "0001", "0005",
"0546", "0001", "0546", "0005", "0004", "0004"), quest = c("A",
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B",
"B", "B", "B", "B", "B"), answ = c("Self", "Self", "Self", "Self",
"Father", "Sister", "Daughter", "Mother", "Father", "Self", "Father",
"X1", "Y1", "Z1", "Y1", "Z2", "X1", "X1", "X1")), class = "data.frame", row.names = c(NA,
-19L))