如果列名称与一列的值与另一个数据帧的列值匹配,如何替换列名称

时间:2015-12-22 02:23:10

标签: r

我有一个名为m的矩阵。我想替换m中的合并名称,如果它们与数据框current中的mydf列中的值匹配,则替换为replacement中的值。如果他们不匹配,我不想改变任何东西。因此,结果中none列没有变化。如果在替换列中存在匹配且可替换的所有内容,我本可以尝试类似(colnames(m) = mydf$replacement[which(mydf$current %in% colnames(m))])的内容,但事实并非如此,因为none中没有替换m列。

m <- matrix(1:9, nrow = 3, ncol = 3, byrow = TRUE,
            dimnames = list(c("s1", "s2", "s3"),c("tom", "dick","none")))

#    tom dick none
#s1   1    2   3
#s2   4    5   6
#s3   7    8   9

current<-c("tom", "dick","harry","bob")
    replacement<-c("x","y","z","b")
    mydf<-data.frame(current,replacement)

mydf
#  current replacement
#1     tom           x
#2    dick           y
#3   harry           z
#4     bob           b

result

#     x    y   none
#s1   1    2   3
#s2   4    5   6
#s3   7    8   9

Attn:akrun - 这是实际数据:

m<-structure(c("chr5:11823", "chr5:11823", "9920035", "9920036", 
"chr5", "chr5", "11823", "11823", "11824", "11824", "sub", "snp", 
"G", "G", "CTAACCCCT", "T", NA, "dbsnp.129:rs55765826", "NN", 
"NN", "NN", "NN", "NN", "NN", "NN", "NN", "NN", "NN", "NN", "NN"
), .Dim = c(2L, 15L), .Dimnames = list(c("1", "2"), c("key", 
"variantId", "chromosome", "begin", "end", "varType", "reference", 
"alleleSeq", "xRef", "GS000038035-ASM", "GS000038036-ASM", "GS000038037-ASM", 
"GS000038038-ASM", "GS000038041-ASM", "GS000038042-ASM")))

mydf <-structure(list(assembly_id = c("GS000038042-ASM", "GS000038041-ASM", 
"GS000038037-ASM", "GS000038038-ASM", "GS000038103-ASM", "GS000038096-ASM", 
"GS000038064-ASM", "GS000038057-ASM", "GS000038062-ASM", "GS000038072-ASM"
), sample_id = c("GS02589-DNA_E06", "GS02589-DNA_F01", "GS02589-DNA_G01", 
"GS02926-DNA_B01", "GS02589-DNA_E08", "GS02589-DNA_F07", "GS02589-DNA_B05", 
"GS02589-DNA_B04", "GS02589-DNA_H04", "GS02589-DNA_H01"), customer_sample_id = c("AMLM12001KP", 
"1114002", "1121501", "1231401", "AMLM12019S-P", "AMLM12014N-R", 
"AMLM12012CA", "1321801", "AMLM12033MD", "1123801"), exomes.ids = c("AMLM12001KP", 
"AMAS-11.3-Diagnostic", "AMAS-12.3-Diagnostic", "AMAS-18.3-Diagnostic", 
"AMLM12019S-P", "AMLM12014N-R", "AMLM12012CA", "AMAS-4.3-Diagnostic", 
"AMLM12033MD", "AMAS-13.3-Diagnostic")), .Names = c("current", 
"customer_sample_id", "assembly_id", "replacement"), row.names = c(NA, 
10L), class = "data.frame")

2 个答案:

答案 0 :(得分:4)

v <- colnames(m) %in% current
w <- current %in% colnames(m)
colnames(m)[v] <- replacement[w]

> m
   x y none
s1 1 2    3
s2 4 5    6
s3 7 8    9

答案 1 :(得分:3)

我们也可以使用match

i1 <- match(colnames(m), mydf$current, nomatch=0)
colnames(m)[i1] <- as.character(mydf$replacement[i1])
m
#   x y none
#s1 1 2    3
#s2 4 5    6
#s3 7 8    9

更新

基于更新的数据集

i2 <- match(mydf$current, colnames(m), nomatch=0)
colnames(m)[i2] <- as.character(mydf$replacement)[i1]