0

I am trying to integrate data by merging two data-set in R, I am wondering if i can use the function merge() but i want to specify the column witch have the same information in one result column here is an example:

dataset 1 : |FirstName | LastName | Age | Eyes-color |

dataset 2 : |FN | LastName | Age | Gender |

i want to merge dataset 1 and dataset 2 in one dataset (dataset 3) and specifying that "FN" is same as "FirstName" so i want it to be one column so the result should be :

dataset 3 : |FirstName | LastName | Age | Eyes-color | Gender |

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
Ishak Hari
  • 148
  • 15

3 Answers3

2

if you want to merge by the FN / FirstName as the common variable the you can do in one dplyr chain to rename and use merge:

dataset3 <- dataset2 %>% 
  rename(FirstName = FN) %>% 
  merge(dataset1, by = "FirstName")
nycrefugee
  • 1,629
  • 1
  • 10
  • 23
1

You can set the same column name and then use it to merge the data.

names(dataset2)[1] = "FirstName"
dataset3 = merge(dataset1, dataset2, by="FirstName")
1

With the dplyr package you can do that with:

dplyr::inner_join(
x = dataset1, y = dataset2,
by = c("FirstName" = "FN")
)