Im folgenden Beispiel habe ich zwei Datensätze (Z und A). Ich möchte diese Sätze mit den ILMN-Nummern zusammenführen oder kombinieren. Wenn es keine Übereinstimmung gibt, geben Sie NA ein.
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")))
Das Ergebnis wird so sein:
[,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
Verwenden Sie match
, um den gewünschten Vektor zurückzugeben, und cbind
in Ihre Matrix
cbind(t, z[, "symbol"][match(rownames(t), rownames(z))])
[,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"
PS. Seien Sie gewarnt dass t
eine Basis-R-Funktion ist, die zum Transponieren von Matrizen verwendet wird. Wenn Sie eine Variable namens t erstellen, kann dies zu Verwirrung in Ihrem Downstream-Code führen.
Verwenden Sie die Funktion zum Zusammenführen und Umbenennen Ihres t-Vektors als tt (siehe das PS von Andrie):
merge(tt,z,by="row.names",all.x=TRUE)[,-(5:8)]
Wenn Sie jetzt mit Datenrahmen statt mit Matrizen arbeiten, wird dies sogar um einiges einfacher:
z <- as.data.frame(z)
tt <- as.data.frame(tt)
merge(tt,z["symbol"],by="row.names",all.x=TRUE)
Nicht perfekt aber nahe:
newcol<-sapply(rownames(t), function(rn){z[match(rn, rownames(z)), 5]})
cbind(data.frame(t), newcol)
sie können -Andrie-Antwort in eine generische Funktion einschließen
mbind<-function(...){
Reduce( function(x,y){cbind(x,y[match(row.names(x),row.names(y)),])}, list(...) )
}
Hier können Sie mehrere Bilder mit Namen als Schlüssel binden
cbind.fill <- function(x, y){
xrn <- rownames(x)
yrn <- rownames(y)
rn <- union(xrn, yrn)
xcn <- colnames(x)
ycn <- colnames(y)
if(is.null(xrn) | is.null(yrn) | is.null(xcn) | is.null(ycn))
stop("NULL rownames or colnames")
z <- matrix(NA, nrow=length(rn), ncol=length(xcn)+length(ycn))
rownames(z) <- rn
colnames(z) <- c(xcn, ycn)
idx <- match(rn, xrn)
z[!is.na(idx), 1:length(xcn)] <- x[na.omit(idx),]
idy <- match(rn, yrn)
z[!is.na(idy), length(xcn)+(1:length(ycn))] <- y[na.omit(idy),]
return(z)
}