那是我的矩阵,我想在这个矩阵中添加一个新列:
> dput(head(res))
structure(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), .Dim = c(6L,
20L), .Dimnames = list(c("AT1G01050_0", "AT1G01050_1", "AT1G01080_0",
"AT1G01080_1", "AT1G01080_2", "AT1G01080_3"), c("10", "33.95",
"58.66", "84.42", "110.21", "134.16", "164.69", "199.1", "234.35",
"257.19", "361.84", "432.74", "506.34", "581.46", "651.71", "732.59",
"817.56", "896.24", "971.77", "1038.91")))
新列将取自下面显示的另一个矩阵:
> dput(head(data2))
structure(c("AT1G01050", "AT1G01080", "AT1G01090", "AT1G01320",
"AT1G01470", "AT1G01800", " 24.5", " 32.6", " 47.2", "198.8",
" 16.5", " 31.7"), .Dim = c(6L, 2L), .Dimnames = list(c("5",
"9", "10", "35", "51", "87"), c("locus", "size2")))
如您所见,名称的一部分(在破折号_之前)是相同的。基于此,我想从size2
列中取出值并将其放入新列中的第一个矩阵。
输出:
10 33.95 58.66 84.42 110.21 134.16 164.69 199.1 234.35 257.19 361.84 432.74 506.34 581.46 651.71 732.59 817.56 896.24
AT1G01050_0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
AT1G01050_1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
AT1G01080_0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
AT1G01080_1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
AT1G01080_2 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
AT1G01080_3 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
971.77 1038.91 size2
AT1G01050_0 0 0 24.5
AT1G01050_1 0 0 24.5
AT1G01080_0 0 0 32.6
AT1G01080_1 0 0 32.6
AT1G01080_2 0 0 32.6
AT1G01080_3 0 0 32.6
希望对你们中的某些人来说不会有困难。
答案 0 :(得分:3)
第一步是从res
:
(roots <- sapply(strsplit(rownames(res), "\\_"), "[", 1))
# [1] "AT1G01050" "AT1G01050" "AT1G01080" "AT1G01080" "AT1G01080" "AT1G01080"
现在您可以使用match
功能来获取正确的尺寸:
cbind(res, as.numeric(data2[match(roots, data2[,1]), 2]))
# 10 33.95 58.66 84.42 110.21 134.16 164.69 199.1 234.35 257.19 361.84 432.74 506.34 581.46
# AT1G01050_0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
# AT1G01050_1 0 0 0 0 0 0 0 0 0 0 0 0 0 1
# AT1G01080_0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
# AT1G01080_1 0 0 0 0 0 0 1 0 0 0 0 0 0 0
# AT1G01080_2 0 0 0 0 0 0 0 0 0 0 1 0 0 0
# AT1G01080_3 0 0 0 0 0 0 0 0 0 0 0 0 0 1
# 651.71 732.59 817.56 896.24 971.77 1038.91
# AT1G01050_0 0 0 0 0 0 0 24.5
# AT1G01050_1 0 0 0 0 0 0 24.5
# AT1G01080_0 0 0 0 0 0 0 32.6
# AT1G01080_1 0 0 0 0 0 0 32.6
# AT1G01080_2 0 0 0 0 0 0 32.6
# AT1G01080_3 0 0 0 0 0 0 32.6
答案 1 :(得分:0)
假设我理解你的问题 - 结果是在数据框“d1”
中d1 <- as.data.frame(res)
d2 <- as.data.frame(data2)
d1$locus <- gsub("_[0-9]*$", "", rownames(d1))
d3 <- merge(d1, d2, by="locus", all.x=T)
d1$size2 <- d3$size2
d1$locus <- NULL