I have two data frames like this:
df.1 <- data.frame(
var.1 = sample(1:10),
code = sample(c("A", "B", "C"), 10, replace = TRUE))
df.2 <- data.frame(
var.2 = sample(1:3),
row.names=c("A","B","C"))
What I need to do is to add a third column df.1$var.2 which, for each value in df.1$code take the value from df.2$var.2 accordingly to their row name.
I got to this point but with no success.. Suggestions?
for (i in 1:length(df.1$code)){
if(df.1$code[i] == rownames(df.2))
df.1$var.2[i] <- df.2$var.2
}