I have 2 data frames with a column that has the same type of values, as shown here:
a <- c(1, 2, 3, 4)
b <- c("John", "James", "James", "John")
df1 <- data.frame(a, b)
c <- c("John", "James")
d <- c("A", "B")
df2 <- data.frame(c, d)
In df1
, I want to turn the John
and James
into A
and B
, using df2
, which shows that John
goes with A
and James
goes with B
. Thus, df1 should look like this:
a b
1 A
2 B
3 B
4 A
I tried using the following code, but it doesn't work:
df1$b[df1$b == df2$c] <- df2$d