2016-05-31 138 views
1

我有一個因素變量的2列這樣的數據幀:計數組合因素和它們的逆同時

V1 <- c("A","B","C","Y","D","E","F","U","G","H","I","J","R") 
V2 <- c("Z","Y","W","B","V","U","T","E","S","R","Q","P","H") 
df <- cbind(V1,V2) 
df 
V1 V2 
[1,] "A" "Z" 
[2,] "B" "Y" 
[3,] "C" "W" 
[4,] "Y" "B" 
[5,] "D" "V" 
[6,] "E" "U" 
[7,] "F" "T" 
[8,] "U" "E" 
[9,] "G" "S" 
[10,] "H" "R" 
[11,] "I" "Q" 
[12,] "J" "P" 
[13,] "R" "H" 

現在我woudl喜歡來算,使用的功能,所有的情況下,V1的組合和V2等於組合V2和V1並返回它們,例如用於DF這個計數將等於3,這樣的:

y <-combinations_inver(df[,1],df[,2]) 

y$Combinations 
"B""Y"= "Y""B" 
"E""U"= "U""E" 
"H""R"= "R""H" 

y$Count 
[1] 3 #because there are three ocurrences (see $Combinations) 

回答

3

一個簡單的方法來做到這一點是:

forwards<-paste(V1,V2) 
backwards<-paste(V2,V1) 

的這兩個「套」的交集會是什麼你正在尋找,但R給出了兩組匹配,所以你需要將長度除以2:

length(intersect(forwards, backwards))/2 
1

我們可以用pminpmax重新排序每一行的元素,然後使用duplicated找到th重複的元素電子指數,獲得子集後unique行,並獲得nrow

m1 <- cbind(pmin(df[,1], df[,2]), pmax(df[,1], df[,2])) 
i1 <- duplicated(m1)|duplicated(m1, fromLast=TRUE) 
nrow(unique(m1[i1,])) 
#[1] 3