-2

I would like to compare the variable "Name" in the dataframes and if the values are the same, print that value, this is what I´m doing:

x <- data.frame("SN" = 1:4, "Age" = c(21,15,30,40), "Name" = c("Isa","Dora","Luisa","Daniela"))
x

y <- data.frame("SN" = 1:4, "Age" = c(22,17,36,41), "Name" = c("Isa","Cristian","Peter","Juan"))
y

y$Name

i

for (i in "y$Name"){
  if (i = "x$Name" ){
    print(i)
  }
 next
}
  • `intersect(x = x$Name,y = y$Name)` – tushaR Feb 20 '18 at 04:15
  • Using for loop: `for (i in y$Name){ if (i %in% x$Name ){ print(i) } }` – tushaR Feb 20 '18 at 04:23
  • This can be helpful. https://stackoverflow.com/questions/3695677/how-to-find-common-elements-from-multiple-vectors – Ronak Shah Feb 20 '18 at 04:52
  • Remove the quotes from first line of for sequence, and replace if condition by `if(x$name[i] == y$name[i])`. This does a rowwise comparison, which is not what current answer or comments give. Can be shortened to `x$name[x$name == y$name]` – moodymudskipper Feb 20 '18 at 07:27

1 Answers1

1

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.

kgolyaev
  • 565
  • 2
  • 10