2012-08-02 39 views
0

我有兩個矩陣從不同的CSV文件中提取,就比較矩陣包括文字/字符

一種是像

> matrix1[1:6,] 
    V1  V2 
1 atheism 1.0000000 
2  rec 1.0000000 
3  alt 1.0000000 
4 baseball 1.0000000 
5  sport 1.0000000 
6 season 0.4934226 

另一個是

>matrix2[1:6,] 
     V1  V2 
1  alt 1.0000000 
2 atheism 1.0000000 
3 baseball 1.0000000 
4  rec 1.0000000 
5  sport 1.0000000 
6   c 0.4934226 

我想要做的是比較兩個矩陣,這是我做的

mapply(as.data.frame(test1),as.data.frame(test2),FUN=function(v1,v2) all(v2==v2)) 
     V1 V2 
     TRUE TRUE 

但是,我需要的是捕獲兩個數據集之間第一列的差異,但是我沒有捕獲到字符串中的差異,如何修改我的代碼。謝謝。

回答

0

也許嘗試:

union(test1[, 1] , test2[, 1]) #the same (together) 
intersect(test2[, 1], test1[, 1]) #overlap 
union(test1[, 1] , test2[, 1]) [!union(test1[, 1] , test2[, 1]) %in% intersect(test2[, 1], test1[, 1])] 
setdiff(test1[, 1] , test2[, 1]) #diff in set 1 
setdiff(test2[, 1] , test1[, 1]) #diff in set 2