2017-07-26 14 views
0

我有以下data.frame計數字符串的數量在一列中共同與其他柱串

V1 | V2 
------+------------ 
a,b,c | x,y,z,w,t,a 
c,d | d,z,c,t 
a,b,d | a,f,t,b,d 

其中V1串的計數總是下3.我需要的R代碼,它告訴常見的字符串與V2數:

V1 | V2   | Count 
------+-------------+------ 
a,b,c | x,y,z,w,t,a | 1 
c,d | d,z,c,t  | 2 
a,b,d | a,f,t,b,d | 3 

回答

1

試試這個:

mapply(function(x,y) sum(x %in% y), 
    strsplit(df$V1,",", fixed=TRUE),strsplit(df$V2,",", fixed=TRUE)) 
#[1] 1 2 3 

數據

df<-structure(list(V1 = c("a,b,c", "c,d", "a,b,d"), V2 = c("x,y,z,w,t,a", 
"d,z,c,t", "a,f,t,b,d")), .Names = c("V1", "V2"), row.names = c(NA, 
-3L), class = "data.frame") 
+0

謝謝@nicola。你可以請建議一種方法來找到%計數也。即V1 | V2 | Count |% ------ + ------------ + ------- + a,b,c | x,y,z, w,t,a | 1 | 33.33 c,d | d,z,c,t | 2 | 100 a,b,d | a,f,t,b,d | 3 | 100 – aman

+0

明白了。謝謝 :) – aman

相關問題