Skip to content

simple matching with R

4 messages · Birgit Lemcke, Jeffrey Robert Spies

#
Not sure how you want to handle the NAs, but you could try the  
following:

#start
MalVar29_37 <- read.table(textConnection("V1 V2 V3 V4 V5 V6 V7 V8 V9
0  0  0  0  0  1  0  0  0
0  0  0  0  0  1  0  0  0
0  0  0  0  0  1  0  0  0
NA NA NA NA NA NA NA NA NA
0  1  0  0  0  1  0  0  0"), header=TRUE)

FemVar29_37 <- read.table(textConnection("     V1 V2 V3 V4 V5 V6 V7  
V8 V9
1  1  0  0  0  0  0  0  0
0  1  0  0  1  1  0  0  0
1  0  0  1  0  0  0  0  0
0  1  0  0  1  0  0  0  0
0  1  0  0  0  0  0  0  0"), header=TRUE)

comparison <- MalVar29_37 == FemVar29_37

dissimilar <- function(tRow){
	length(tRow[tRow==FALSE])
}

dissimilarity <- apply(comparison, c(1), dissimilar)
dissimilarity
# finish

Variable comparison is an entry by entry comparison, resulting in  
values of TRUE or FALSE.  I've defined a function dissimilar as the  
number of FALSEs in a given object (tRow).  Variable dissimilarity is  
then the application of this dissimilar function for each row of  
comparison.  In this example, 0 means all of the entries in a row  
matche, 9 means none of them matched.  You can see the solution here  
in recipe form: http://www.r-cookbook.com/node/40

Hope this helps,

Jeff.
On Sep 28, 2007, at 11:13 AM, Birgit Lemcke wrote: