我是R新手,我的数据如下
NAME
----
Balachandra
Sunita
Kali
Jagmeet
我想将其作为
NAME1 NAME2
-----------------------
Balachandra Balachandra
Balachandra Sunita
Balachandra Kali
Balachandra Jagmeet
Sunita Balachandra
Sunita Sunita
Sunita Kali
Sunita Jagmeet
Kali Balachandra
Kali Sunita
Kali Kali
Kali Jagmeet
Jagmeet Balachandra
Jagmeet Sunita
Jagmeet Kali
Jagmeet Jagmeet
拥有名称的所有可能组合
知道怎么做吗?
答案 0 :(得分:2)
我们可以使用expand.grid
expand.grid(NAME1=df1$NAME, NAME = df1$NAME)[2:1]
# NAME NAME1
#1 Balachandra Balachandra
#2 Balachandra Sunita
#3 Balachandra Kali
#4 Balachandra Jagmeet
#5 Sunita Balachandra
#6 Sunita Sunita
#7 Sunita Kali
#8 Sunita Jagmeet
#9 Kali Balachandra
#10 Kali Sunita
#11 Kali Kali
#12 Kali Jagmeet
#13 Jagmeet Balachandra
#14 Jagmeet Sunita
#15 Jagmeet Kali
#16 Jagmeet Jagmeet
或使用CJ
data.table
library(data.table)
setDT(df1, key = "Name")[CJ(Name, Name)]
df1 <- data.frame(NAME = c("Balachandra", "Sunita", "Kali",
"Jagmeet"), stringsAsFactors=FALSE)