Mari
Mari

Reputation: 49

How to add data from one data set to another in R?

Hi I would like to merge two data sets, and intergrate the data from one data set (Df2$IgA_Titre) into the same column of my original dataset, Df1, which is missing the data.

Unfortunately as they are the common variables, I think R tries to merge based off IgA_titre as well, even when I specify the variables to merge by. As a result the data isn't added and I still have missing values for IgA.

I have tried the below code and various dplyr/mutate alternatives but it still cant intergrate the data

test <- left_join(Df1, Df2, by = c("Sample_ID", "Visit"))

#Df1
    Sample_ID    Visit IgA_Titre Vi.IgG1.G0
1      8016  Pre-Vac        NA  0.12     
2      8016 Post-Vac        NA  0.15 
3      8034  Pre-Vac        NA  0.13  
4      8034 Post-Vac        NA  0.17  


#Df2
    Sample_ID   Visit IgA_Titre
1      8016 Pre-Vac    1.5625
2      8016 Post-Vac   1.5625
3      8034 Pre-Vac    1.5625
4      8034 Post-Vac   11.5965


Any insight would be much appreciated!

Upvotes: 1

Views: 455

Answers (1)

Sathish
Sathish

Reputation: 12723

library(data.table)
setDT(df1)[, IgA_Titre := as.numeric(IgA_Titre)]  # tidy IgA_Titre column as numeric type
df1[df2, IgA_Titre := i.IgA_Titre, on = .(Sample_ID, Visit)] # perform join
df1
#   Sample_ID    Visit IgA_Titre Vi.IgG1.G0
#1:      8016  Pre-Vac    1.5625       0.12
#2:      8016 Post-Vac    1.5625       0.15
#3:      8034  Pre-Vac    1.5625       0.13
#4:      8034 Post-Vac   11.5965       0.17

Upvotes: 1

Related Questions