簡體   English   中英

如果列名稱與一列的值與另一個數據幀的列值匹配,如何替換列名稱

[英]How to replace column names if it matches with the values of one column with the column values of another dataframe

我有一個名為m的矩陣。 我想替換colnames m ,如果他們匹配的值current在數據幀列mydf與中值替換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

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")
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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM