gpt4 book ai didi

r - 按行名合并或组合

转载 作者:行者123 更新时间:2023-12-03 06:12:30 25 4
gpt4 key购买 nike

在下面的示例中,我有两个数据集(Z 和 A)。我想通过 ILMN 编号合并或组合这些集合。如果没有匹配,则填写NA。

z <- matrix(c(0,0,1,1,0,0,1,1,0,0,0,0,1,0,1,1,0,1,1,1,1,0,0,0,"RND1","WDR", "PLAC8","TYBSA","GRA","TAF"), nrow=6,
dimnames=list(c("ILMN_1651838","ILMN_1652371","ILMN_1652464","ILMN_1652952","ILMN_1653026","ILMN_1653103"),c("A","B","C","D","symbol")))

t<-matrix(c("GO:0002009", 8, 342, 1, 0.07, 0.679, 0, 0, 1, 0,
"GO:0030334", 6, 343, 1, 0.07, 0.065, 0, 0, 1, 0,
"GO:0015674", 7, 350, 1, 0.07, 0.065, 1, 0, 0, 0), nrow=10, dimnames= list(c("GO.ID","LEVEL","Annotated","Significant","Expected","resultFisher","ILMN_1652464","ILMN_1651838","ILMN_1711311","ILMN_1653026")))

结果将是这样的:

             [,1]         [,2]         [,3]         [,4]
GO.ID "GO:0002009" "GO:0030334" "GO:0015674" NA
LEVEL "8" "6" "7" NA
Annotated "342" "343" "350" NA
Significant "1" "1" "1" NA
Expected "0.07" "0.07" "0.07" NA
resultFisher "0.679" "0.065" "0.065" NA
ILMN_1652464 "0" "0" "1" PLAC8
ILMN_1651838 "0" "0" "0" RND1
ILMN_1711311 "1" "1" "0" NA
ILMN_1653026 "0" "0" "0" GRA

最佳答案

使用合并并将 t 向量重命名为 tt (请参阅 Andrie 的 PS):

merge(tt,z,by="row.names",all.x=TRUE)[,-(5:8)]

现在,如果您使用数据框而不是矩阵,这甚至会变得容易得多:

z <- as.data.frame(z)
tt <- as.data.frame(tt)
merge(tt,z["symbol"],by="row.names",all.x=TRUE)

关于r - 按行名合并或组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6029743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com