简体   繁体   中英

combine many columns from one dataframe into another dataframe using setDT

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM