Home > Software engineering >  Replace values in df1 with values from another dataset (df2)
Replace values in df1 with values from another dataset (df2)

Time:10-08

This is the question:

** df1 ==> You have one dataset:

with df1$IDs from 1:100, (each ID appears twice df$visit), a column called df1$weight and a column called df1$height

** df2==> another dataset:

with df2$IDs from 1:50 (each ID appears twice df$visit), and a column called df2$weight.

And you want to create a THIRD dataset where you will have:

exactly the same dataset as in df1 but for those IDs that are present in df2 you replace df1$weight for df2$weight. Obviously taking into account visit.

How would you do that?

Thanks!

CodePudding user response:

We may do a join

library(data.table)
df3 <- copy(df1)
setDT(df3)[df2, weight := i.weight, on = .(IDs)]

If it is more than one column, we may do

setDT(df3)[df2, c('weight1', 'weight2') := .(i.weight1, i.weight2), on = .(IDs)]

If there are many columns, create an vector of those column names

nm1 <- names(df2)[1:5] # suppose if the first five column names wanted
nm2 <- paste0("i.", nm1) # for the corresponding column names from second data
setDT(df3)[df2, (nm1) := mget(nm2), on = .(IDs)]
  • Related