[R] simple matching with R

Jeffrey Robert Spies jspies at nd.edu
Fri Sep 28 18:25:00 CEST 2007


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:

> Hello!
>
> I am R beginner and I have a question obout a simple matching.
>
> I have to datasets that i read in with:
>
> MalVar29_37<-read.table("MalVar29_37.csv", sep = ";")
> FemVar29_37<-read.table("FemVar29_37.csv", sep = ";")
>
> They look like this and show binary variables:
>
>      V1 V2 V3 V4 V5 V6 V7 V8 V9
> 1    0  0  0  0  0  1  0  0  0
> 2    0  0  0  0  0  1  0  0  0
> 3    0  0  0  0  0  1  0  0  0
> 4   NA NA NA NA NA NA NA NA NA
> 5    0  1  0  0  0  1  0  0  0
>
>      V1 V2 V3 V4 V5 V6 V7 V8 V9
> 1    1  1  0  0  0  0  0  0  0
> 2    0  1  0  0  1  1  0  0  0
> 3    1  0  0  1  0  0  0  0  0
> 4    0  1  0  0  1  0  0  0  0
> 5    0  1  0  0  0  0  0  0  0
>
> each with 348 rows.
>
> I would like to perform a simple matching but only row 1 compared to
> row1, row 2 compared to row 2 (paired).......giving back a number as
> dissimilarity for each comparison.
>
> How can i do that?
>
> Thanks in advance
>
> Birgit
>
>
>
>
> Birgit Lemcke
> Institut für Systematische Botanik
> Zollikerstrasse 107
> CH-8008 Zürich
> Switzerland
> Ph: +41 (0)44 634 8351
> birgit.lemcke at systbot.uzh.ch
>
>
>
>
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting- 
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list