The question sounds a bit generic but I think an example would be much clearer:
I have the following two data frames
data1
group1 group2 group3 Level
cat cat dog 1
dog parrot cat 1
mouse dolphin dolphin 1
red blue blue 2
green yellow green 2
black purple cat 2
data2
var1 level Score
cat 1 1
dog 1 1
mouse 1 1
dolphin 1 0
parrot 1 1
red 2 1
blue 2 1
green 2 1
purple 2 1
cat 2 0
black 2 0
yellow 2 1
I want to modify data1 incorporating 3 new columns (one for each group1, group2 and group3) with the values I find in the column "score" from data2 depending on the levels of "level" (level is a factor). So basically I want to obtain something like this:
group1 group2 group3 Level var1 var2 var3
cat cat dog 1 1 1 1
dog parrot cat 1 1 1 1
mouse dolphin dolphin 1 1 0 0
red blue blue 2 1 1 1
green yellow green 2 1 1 1
black purple cat 2 0 1 0
Sample Data
df1 <- structure(list(
group1 = c("cat", "dog", "mouse", "red", "green", "black"),
group2 = c("cat", "parrot", "dolphin", "blue", "yellow", "purple"),
group3 = c("dog", "cat", "dolphin", "blue", "green", "cat"),
Level = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("1", "2"), class = "factor")),
row.names = c(NA, -6L), class = "data.frame")
df2 <- structure(list(
var1 = c("cat", "dog", "mouse", "dolphin", "parrot", "red", "blue", "green", "purple", "cat", "black", "yellow"),
level = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("1", "2"), class = "factor"),
Score = c(1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L)),
row.names = c(NA, -12L), class = "data.frame")