【问题标题】:How to replace column names if it matches with the values of one column with the column values of another dataframe如果列名与一个列的值与另一个数据框的列值匹配,如何替换它
【发布时间】:2015-12-22 17:09:39
【问题描述】:

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

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

收件人: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")

【问题讨论】:

    标签: r


    【解决方案1】:
    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
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 不知道为什么给我的实际数据错误:Error in colnames(*tmp*, value = c("AMLM12001KP", "AMAS-11.3-Diagnostic", : 'names' attribute [123] must be the same length as the vector [119]
      • @MAPK 你应该展示了一个模仿你的数据集的数据集。
      • 谢谢,我喜欢您提出解决方案的方式,但似乎不适用于我的数据。不得不求助于蒂姆的解决方案。
      • @MAPK 您能否用一个新示例更新您的问题,以便我也可以更新答案,因为我认为match 更通用,并且在列不按顺序时应该可以工作。
      • @MAPK 更新了帖子。现在,我没有收到任何错误。
      猜你喜欢
      • 2022-11-25
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 2019-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多