Multiple join on R

Asked

Viewed 181 times

3

I have two tables: dados and dados_aux. I need to add to the table dados the column REGIÃO, present in the table dados_aux. However, I have to use two keys to make the Join: CIDADE and UF. Otherwise, the information will be incorrect. It is possible to make this Join with two keys using the merge()? If yes, how would it be?

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")

1 answer

9


You can specify the columns by argument by, by.x and by.y (if the names of the variables are different between the data.frame). Thus,

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

should give you the expected result.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.