Consider the following data frames:
df <- data.frame(x = c("A", "A", "A", "B", "C", "C"),
y = c("abl", "rtg", "jaf", "rlt", "thk", "lpv"))
z = c(rep("abl", 4), rep("rtg", 2), rep("jaf",1), rep("zfw", 3), "thk")
dat <- data.frame(z = z, group = rep(NA, length(z)))
I want dat$group
to be filled with the value of df$x
from that row, where the value of df$y
matches dat$z
. The final data frame should look like that:
z group
abl A
abl A
abl A
abl A
rtg A
rtg A
jaf A
zfw NA
zfw NA
zfw NA
thk C
I just can't figure out how to do this.
The code that I tried so far:
dat$group[which(dat$z == df$y)] <- df$x[which(df$y == dat$z)]
dat$group[which(dat$z %in% df$y)] <- df$x[which(df$y %in% dat$z)]
It is throwing an error and not producing the desired result. How can I get the final data frame?