我有这样的数据:
ID=c(rep("ID1",3), rep("ID2",2), "ID3", rep("ID4",2))
sex=c(rep("male",3), rep("female",2), "female", rep("male",2))
item=c("a","b","c","a","c","a","b","a")
df1 <- data.frame(ID,sex,item)
df1
ID sex item
1 ID1 male a
2 ID1 male b
3 ID1 male c
4 ID2 female a
5 ID2 female c
6 ID3 female a
7 ID4 male b
8 ID4 male a
我需要它作为这样的边缘:
head(nodes)
ID sex V1 V2
1 ID1 male a b
2 ID1 male b c
3 ID1 male a c
4 ID2 female a c
5 ID4 male b a
在@akrun 的帮助下,我可以获得 V1 和 V2 列:
lst <- lapply(split(item, DG), function(x) if(length(x) >=2) t(combn(x,2)) else NULL)
nodes=as.data.frame(do.call(rbind,lst[!sapply(lst, is.null)]) )
但是我怎么能从原始df中“带走”ID和一些其他变量(性别、年龄等)并将它们作为“节点”中的“性别”等列?