Join multiple in R

3

I have two tables: dados and dados_aux . I need to add to the table dados the REGIÃO column, present in the dados_aux table. However, I have to use two keys to join: CIDADE and UF . Otherwise, the information will be incorrect. Is it possible to do this join with two keys using merge() ? If so, what would it be like?

Table dados :

structure(list(CIDADE = structure(c(4L, 5L, 1L, 2L, 3L), .Label = c("BOA VISTA", 
"MANAUS", "RECIFE", "RIO DE JANEIRO", "SAO PAULO"), class = "factor"), 
    UF = structure(c(3L, 5L, 4L, 1L, 2L), .Label = c("AM", "PE", 
    "RJ", "RR", "SP"), class = "factor")), .Names = c("CIDADE", 
"UF"), row.names = c(NA, -5L), class = "data.frame")

Table dados_aux :

structure(list(CIDADE = structure(c(4L, 5L, 1L, 1L, 2L, 3L, 3L
), .Label = c("BOA VISTA", "MANAUS", "RECIFE", "RIO DE JANEIRO", 
"SAO PAULO"), class = "factor"), UF = structure(c(4L, 7L, 5L, 
2L, 1L, 6L, 3L), .Label = c("AM", "MG", "PE", "RJ", "RR", "SC", 
"SP"), class = "factor"), REGIAO = structure(c(3L, 3L, 2L, 3L, 
2L, 4L, 1L), .Label = c("NORDESTE", "NORTE", "SUDESTE", "SUL"
), class = "factor")), .Names = c("CIDADE", "UF", "REGIAO"), row.names = c(NA, 
-7L), class = "data.frame")
    
asked by anonymous 31.01.2018 / 14:58

1 answer

9

You can specify the columns using the argument by , by.x and by.y (if variable names are different between data.frame ). In this way,

merge(dados, dados_aux, by = c("CIDADE", "UF"))

It should give you the expected result.

    
31.01.2018 / 15:45