-3

Here are my datasets:

df1:

HT AT HHAI HAAI HHGC HAGC

AS Ar 0.3 0.5 0.8 0.9

and so on.

I'm doing this in R.

df2:

HT AT HHAI HAAI HHGC HAGC

BS FS NA NA NA NA

and so on.

I want to copy values of HHAI, HAAI, HHGC, HAGC from df1 into same columns of df2 when:
df1.HT==df2.HT and df1.AT==df2.AT

df1 and df2 are large dataframes.

I'm very new to R. Don't know about much methods and all.

Please post the code here as I don't have much knowledge about R.

Thanks in advance.

Kavach Chandra
  • 770
  • 1
  • 10
  • 25
  • 2
    Provide better example and a example how you want your output to look. Also, check in merge function or different joins in dplyr package. – MLEN Apr 02 '17 at 18:55
  • you can try `merge(df1, df2, by = "column_name")` – Mislav Apr 02 '17 at 19:00
  • @Mislav there are two columns from both data frames which I want to compare. Basically, if value in df1.HT== df2.HT && df1.AT == df2.AT then only data should be copied from df1's column to df2's column. – Kavach Chandra Apr 02 '17 at 19:07
  • 2
    than use vector of columns to merge: `merge(df1, df2, by = c("column_name", "column_name2), all.x = TRUE, all.y = FALSE)` – Mislav Apr 02 '17 at 19:16

1 Answers1

3

Solution:

merge(df1, df2, by = c("column_name", "column_name2"), all.x = TRUE, all.y = FALSE)
Mislav
  • 1,533
  • 16
  • 37