Generally, the right way to handle such problems is through merge
or join semantics:
# added stringsAsFactors=FALSE to avoid treating names as factors
x <- data.frame("SN" = 1:4,
"Age" = c(21,15,30,40),
"Name" = c("Isa","Dora","Luisa","Daniela"),
stringsAsFactors = FALSE)
y <- data.frame("SN" = 1:4,
"Age" = c(22,17,36,41),
"Name" = c("Isa","Cristian","Peter","Juan"),
stringsAsFactors = FALSE)
z <- merge(x, y, by="Name")[["Name"]]
z
[1] "Isa"
That being said, I don't recommend joining two data sets by names (or do any kind of comparison), for a couple reasons. First, subtle spelling issues will cause you to not match records that you would probably want matched (try converting names in y
into lowercase and rerunning the code). Second, names are frequently non-unique, and you will have to solve the herculean task of deduplicating the data.