+5 votes
in Programming Languages by (40.5k points)
Two data frames have one common column. I want to merge them by that common column. Which R function should I use to merge them?

1 Answer

+1 vote
by (354k points)
selected by
 
Best answer

R function merge() can be used to merge two data frames by common column or row names. You can find the details about this function here. Using the "by" argument, you can specify the common column name.

Here is an example to show how to use this function:

df1 <- data.frame(Name=c('A', 'B', 'C', 'D', 'E', 'F'), DOB=c(2020,2017,2012,1980,1998,1967))

df2 <- data.frame(Name=c('A', 'B', 'C', 'D', 'E', 'F'), Income=c(12020,22017,22012,31980,11998,21967))

df <- merge(df1, df2, by.x = "Name")

The output of the above code:

> df
  Name  DOB Income
1    A 2020  12020
2    B 2017  22017
3    C 2012  22012
4    D 1980  31980
5    E 1998  11998
6    F 1967  21967

Related questions


...