# [R] matching vectors against vectors

From: Piet van Remortel <piet.vanremortel_at_gmail.com>
Date: Wed 30 Mar 2005 - 06:47:02 EST

I have a re-occuring typical problem that I don't know how to solve efficiently.

The situation is the following: I have a number of data-sets (A,B,C,...) , consisting of an identifier (e.g. 11,12,13,...,20) and a measurement (e.g. in the range 100-120). I want to compile a large table, with all availabe identifiers in all data-sets in the rows, and a column for every dataset.

Now, not all datasets have a measurement for every identifier, so I want NA if the set does not contain the identifier.

an example for a single dataset:

#all identifiers
> rep <- c(10:20)

#Identifiers in my dataset (a subset of rep)
> rep1 <- c(12,13,15,16,17,18)

#measurements in this dataset
> rep1.r <- c(112,113,115,116,117,118)

#a vector which should become a column in the final table, now containing all NAs
> res <- rep(NA,10)

#the IDs and values of my dataset together
> data <- cbind(rep1, rep1.r)

data looks like this:

rep1 rep1.r

```[1,]   12    112
[2,]   13    113
[3,]   15    115
[4,]   16    116
[5,]   17    117
[6,]   18    118

```

Now, I want to put the values 112, 113, 115,... in the correct rows of the final table, using the identifiers as an indicator of which row to put it in, so that I finally obtain:

I try to avoid repeating 'which' a lot and filling in every identifier's observation etc, since I will be doing this for thousands of rows at once. There must be an efficient way using factors, tapply etc, but I have trouble finding it. Ideal would be if this could be done in one go, instead of looping.

Any suggestions ?

Thanks,

Piet

R-help@stat.math.ethz.ch mailing list