R 如何匹配两个数据帧中的列值,并使行名具有匹配的相应列值
我有一个名为R 如何匹配两个数据帧中的列值,并使行名具有匹配的相应列值,r,R,我有一个名为mydf的数据帧。我想将另一个名为secondf的数据帧中的当前列与列key.genomloc匹配,并提取相应的key.wesmut.genom列值,并使该行名如结果所示 这是我尝试过的,但没有达到预期效果: current <- secondf[,"key.genomloc"] replacement <- secondf[,"key.wesmut.genom"] v <- mydf[,"current"] %in% current w <- current
mydf
的数据帧。我想将另一个名为secondf
的数据帧中的当前列与列key.genomloc
匹配,并提取相应的key.wesmut.genom
列值,并使该行名如结果所示
这是我尝试过的,但没有达到预期效果:
current <- secondf[,"key.genomloc"]
replacement <- secondf[,"key.wesmut.genom"]
v <- mydf[,"current"] %in% current
w <- current %in% mydf[,"current"]
rownames(mydf)<-mydf[,"current"]
rownames(mydf)[v] <- replacement[w]
我们可以使用match
mydf$rowname <- secondf[,1][match(mydf$current,secondf[,2])]
mydf[c(3,1:2)]
# rowname current index
#1 WES:FLT3:p.D835A chr1:115258747:C:T 1451738
#2 WES:FLT3:p.D835H chr1:115256530:G:T 1451718
mydf$rowname这将是一个类似的答案,对吗cbind(rowname=secondf[which(secondf[,2]%在%mydf$current中),1],mydf)
不值得作为单独的答案发布。@RonakShah对于这种匹配,我不会在%
中使用%,但如果您认为它是正确的,您可以将其作为答案发布(尚未测试)
rowname current index
WES:FLT3:p.D835A chr1:115258747:C:T 1451738
WES:FLT3:p.D835H chr1:115256530:G:T 1451718
mydf$rowname <- secondf[,1][match(mydf$current,secondf[,2])]
mydf[c(3,1:2)]
# rowname current index
#1 WES:FLT3:p.D835A chr1:115258747:C:T 1451738
#2 WES:FLT3:p.D835H chr1:115256530:G:T 1451718