Search code examples
rdataframemergeintersect

How to combine dataframes in R


I'd like to combine two dataframe in a new one which contain columns from both of them, moreover I need to put in the new dataframe only rows with same id.

my dataframes looks like:

df1
Name       V1  V2   V3
str1       .   .    strA    
str2       .   .    strB
..         .   . 
str16000   .   .    strC


df2
Name       V1  V2   V3
str2       .   .    strD    
str1       .   .    strE
..         .   . 
str20000   .   .    strF

I'd like an output like:

Name     df1$v3    df2$v3
str1     strA      strE
str2     strB      strD

Note that df1 and df2 have different lenghts, moreover the same item in df1 and df2 has not the same position.

thanks you guys


Solution

  • Use the merge function

    lines=
       'Name      V1  V2   V3
        str1      NA  NA   strA    
        str2      NA  NA   strB
        str16000  NA  NA   strC'
    
    df1 = read.table(textConnection(lines), header = T)
    
    lines=
       'Name      V1  V2   V3
        str1      NA  NA   strD    
        str2      NA  NA   strE
        str16000  NA  NA   strF'
    
    df2 = read.table(textConnection(lines), header = T)
    
    
    dfnew = merge(df1[1:2, -2:-3], df2[1:2, -2:-3], by='Name')
    
    colnames(dfnew) = c('Name', 'df1$v3 ', 'df2$v3')
    
    dfnew 
    
    #  Name df1$v3  df2$v3
    #1 str1    strA   strD
    #2 str2    strB   strE