0

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

Maleda
  • 3
  • 1
  • How about `innerjoin` `library(dplyr); inner_join(df1[c("Name", "V3")], df2[c("Name", "V3")], by = "Name")` or use `merge` from `base R` – akrun Jul 30 '18 at 14:39
  • 1
    Possible duplicate of [How can I combine two dataframes with different lengths in R?](https://stackoverflow.com/questions/37257572/how-can-i-combine-two-dataframes-with-different-lengths-in-r) – A. Suliman Jul 30 '18 at 14:41

1 Answers1

0

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
Thiago Fernandes
  • 273
  • 2
  • 12