Reputation: 5719
I have a matrix called m
. I want to replace the colnames in m
if they match the values in current
column in dataframe mydf
replacing with the values in replacement
. I don't want to change anything if they don't match. So there is no change in none
column in the result. I could have tried something like (colnames(m) = mydf$replacement[which(mydf$current %in% colnames(m))]
) if there was everything matching and replaceable in replacement column, which is not the case as there is no replacement for none
column in 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--here is the actual data:
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")
Upvotes: 3
Views: 1921
Reputation: 886928
We could also use 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
Based on the updated dataset
i2 <- match(mydf$current, colnames(m), nomatch=0)
colnames(m)[i2] <- as.character(mydf$replacement)[i1]
Upvotes: 4
Reputation: 520878
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
Upvotes: 4