[R] Comparing data frames

Laura Ferrero-Miliani laurafe at gmail.com
Sun Apr 18 18:30:02 CEST 2010


Dear very helpful friends,

It is Sunday, there is no air traffic in Europe, what better to do
than try and learn me some more R.
I have the following example:

owner <- c(1:4)
animal <- c("cat", "dog", "cat", "dog")
char.1 <- c("fluffy", "playful", "mean", "stupid")
food <- c("cat food", "left-overs", "cat food", "dog food")
char.2 <- c("lazy", "destructive", "antisocial", "goofy")
color <- c("white", "brown", "black", "black")
char.3 <- c("fat", "tiny", "evil", "big")
age <- c(16, 2, 5, 10)

pet.data <- data.frame(owner, animal, char.1, food, char.2,
                                  color, char.3, age)

animal <- c("cat", "dog")
v1 <- c("fluffy", "big")
v2 <- c("fat", "stupid")

pet.key <- data.frame(animal, v1, v2)


Now I would like to compare my pet.key to my pet.data and add a
variable to pet.data with that result.
So for each "cat" in pet.data, char.1, char.2 and char.3 should
contain "fluffy" AND "fat" for a complete match, or "fluffy" OR "fat"
for a partial match, and so on.

I don't know where to start. I *think* I should be using %in%, but I
don't know how to build the expression so it works (so far I have
tried and have gotten from lists to a weird array as a result!)

btw, what if my data and my key contains repeated values e.g.

animal v1      v2
cat      fluffy fluffy


Any suggestions?


Thanks in advance,

Laura



More information about the R-help mailing list