Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge or combine by rownames

Tags:

merge

dataframe

r

In the example below I have two datasets (Z and A). I want to merge or combine these sets by the ILMN numbers. If there is no match, fill in 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"))) 

The result will be like this:

             [,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 
like image 871
Lisann Avatar asked May 17 '11 10:05

Lisann


People also ask

Can you merge by Rownames in R?

The merge() function in base R can be used to merge input dataframes by common columns or row names. The merge() function retains all the row names of the dataframes, behaving similarly to the inner join. The dataframes are combined in order of the appearance in the input function call.

How do I combine multiple data frames in R?

Join Multiple R DataFrames To join more than two (multiple) R dataframes, then reduce() is used. It is available in the tidyverse package which will convert all the dataframes to a list and join the dataframes based on the column.

Does Cbind match row names?

Cbind: Combine objects by columns matching the rows on row names in mbojan/mbtools: Chaotic Collection of Functions and Datasets Possibly Useful Also To Others.

How does Rbind work in R?

rbind() function in R Language is used to combine specified Vector, Matrix or Data Frame by rows. deparse. level: This value determines how the column names generated. The default value of deparse.


1 Answers

Using merge and renaming your t vector as tt (see the PS of Andrie) :

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

Now if you would work with dataframes instead of matrices, this would even become a whole lot easier :

z <- as.data.frame(z) tt <- as.data.frame(tt) merge(tt,z["symbol"],by="row.names",all.x=TRUE) 
like image 160
Joris Meys Avatar answered Oct 28 '22 19:10

Joris Meys