我想写一个 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")]
)
我熟悉将先前的功能与单个变量一起使用,并且我不想在此处使用merge
或join
。提前致谢。
如果要在单个on
联接中执行此操作,请创建感兴趣的列以及第二个数据的列名,然后立即联接'Serial.ID and assign (
:=`)
nm1 <- c("diabetes", "hypertension", "cardiac", "hepatic")
nm2 <- paste0("i.", nm1)
setDT(df)[df1, (nm1) := mget(nm2), on = .(Serial.ID)]
-输出
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
多谢。这行得通。赞成并接受作为解决方案。