Warm tip: This article is reproduced from serverfault.com, please click

combine many columns from one dataframe into another dataframe using setDT

发布于 2020-11-28 00:23:41

I want to write a function

df<-read.table( text="Serial.ID name
1   a
2   c
3   f
4   h
5   u
", sep="\t", header=T)

df1<-read.table( text="Serial.ID    diabetes    hypertension    cardiac hepatic
1   yes 1   1   0
2   no  0   1   1
3   yes 0   1   0
4   no  0   0   1
5   no  1   0   0
", sep="\t", header=T)

require(data.table) ;require(dplyr) 
intersect(names(df), names(df1))

setDT(df)[setDT(df1), diabetes  := i.diabetes, on=c("Serial.ID")]
setDT(df)[setDT(df1), hypertension  := i.hypertension, on=c("Serial.ID")]
setDT(df)[setDT(df1), cardiac  := i.cardiac, on=c("Serial.ID")]
setDT(df)[setDT(df1), hepatic  := i.hepatic, on=c("Serial.ID")]
#############################################################################
# I tried this code but it doesn't work

var<-c("diabetes","hypertension", "cardiac","hepatic"  )
x<-function(
  setDT(df)[setDT(df1), var  := i.var, on=c("Serial.ID")]
  
)
  

I am familiar with using the prior function with a single variable and I do not want to use merge or join here. Thanks in advance.

Questioner
Mohamed Rahouma
Viewed
0
akrun 2020-11-28 08:25:29

If we want to do this in a single join, create the columns of interest, along with the column names of the second data, then do the join on the 'Serial.ID and assign (:=`) at once

nm1 <- c("diabetes", "hypertension", "cardiac", "hepatic")
nm2 <- paste0("i.", nm1)
setDT(df)[df1, (nm1) := mget(nm2), on = .(Serial.ID)]

-output

df
#   Serial.ID name diabetes hypertension cardiac hepatic
#1:         1    a      yes            1       1       0
#2:         2    c       no            0       1       1
#3:         3    f      yes            0       1       0
#4:         4    h       no            0       0       1
#5:         5    u       no            1       0       0