如何匹配两个数据框中的列值,并使用匹配的相应列值生成rownames

时间:2016-01-19 09:48:51

标签: r

我有一个名为mydf的数据框。我希望将名为secondf的另一个数据框中的当前列与列key.genomloc进行匹配,并提取相应的key.wesmut.genom列值,并按result中所示生成该rowname。

这是我尝试过的,但不能按预期工作:

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]

数据:

  mydf <-structure(list(current = structure(c(5L, 2L), .Label = c("chr1:115256529:T:C", 
"chr1:115256530:G:T", "chr1:115258744:C:A", "chr1:115258744:C:T", 
"chr1:115258747:C:T", "chr11:32417945:T:C", "chr12:25398284:C:A", 
"chr12:25398284:C:T", "chr13:28592640:A:C", "chr13:28592641:T:A", 
"chr13:28592642:C:A", "chr13:28592642:C:G", "chr15:90631838:C:T", 
"chr15:90631934:C:T", "chr2:209113112:C:T", "chr2:209113113:G:A", 
"chr2:209113113:G:C", "chr2:209113113:G:T", "chr2:25457242:C:T", 
"chr2:25457243:G:A", "chr2:25457243:G:T", "chr4:55599320:G:T"
), class = "factor"), `index` = c(1451738, 1451718)), .Names = c("current", 
"index"), row.names = 1:2, class = "data.frame")



secondf<-structure(c("WES:FLT3:p.D835H", "WES:FLT3:p.D835N", "WES:FLT3:p.D835Y", 
            "WES:FLT3:p.D835A", "WES:FLT3:p.D835V", "chr1:115256530:G:T", 
            "chr13:28592642:C:T", "chr13:28592642:C:A", "chr1:115258747:C:T", 
            "chr13:28592641:T:A"), .Dim = c(5L, 2L), .Dimnames = list(NULL, 
                                                                      c("key.wesmut.genom", "key.genomloc")))

结果

  rowname                    current   index
WES:FLT3:p.D835A  chr1:115258747:C:T   1451738
WES:FLT3:p.D835H  chr1:115256530:G:T   1451718

1 个答案:

答案 0 :(得分:1)

我们可以使用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