Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lookup values in a vectorized way

Tags:

r

I keep reading about the importance of vectorized functionality so hopefully someone can help me out here.

Say I have a data frame with two columns: name and ID. Now I also have another data frame with name and birthplace, but this data frame is much larger than the first, and contains some but not all of the names from the first data frame. How can I add a third column to the the first table that is populated with birthplaces looked up using the second table.

What I have is now is:

corresponding.birthplaces <- sapply(table1$Name, 
   function(name){return(table2$Birthplace[table2$Name==name])})

This seems inefficient. Thoughts? Does anyone know of a good book/resource for using R 'properly'..I get the feeling that I generally do think in the least computationally effective manner conceivable.

Thanks :)

like image 846
JoshDG Avatar asked Nov 08 '11 21:11

JoshDG


1 Answers

See ?merge which will perform a database link merge or join.

Here is an example:

set.seed(2)
d1 <- data.frame(ID = 1:5, Name = c("Bill","Bob","Jessica","Jennifer","Robyn"))
d2 <- data.frame(Name = c("Bill", "Gavin", "Bob", "Joris", "Jessica", "Andrie", 
                          "Jennifer","Joshua","Robyn","Iterator"),
                 Birthplace = sample(c("London","New York",
                                       "San Francisco", "Berlin",
                                       "Tokyo", "Paris"), 10, rep = TRUE))

which gives:

> d1
  ID     Name
1  1     Bill
2  2      Bob
3  3  Jessica
4  4 Jennifer
5  5    Robyn
> d2
       Name    Birthplace
1      Bill      New York
2     Gavin         Tokyo
3       Bob        Berlin
4     Joris      New York
5   Jessica         Paris
6    Andrie         Paris
7  Jennifer        London
8    Joshua         Paris
9     Robyn San Francisco
10 Iterator        Berlin

Then we use merge() to do the join:

> merge(d1, d2)
      Name ID    Birthplace
1     Bill  1      New York
2      Bob  2        Berlin
3 Jennifer  4        London
4  Jessica  3         Paris
5    Robyn  5 San Francisco
like image 70
Gavin Simpson Avatar answered Oct 09 '22 20:10

Gavin Simpson