Merge or Combine by Rownames

Merge or combine by rownames

Use match to return your desired vector, then cbind it to your 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. Be warned that t is base R function that is used to transpose matrices. By creating a variable called t, it can lead to confusion in your downstream code.

Merge data frames based on rownames in R

See ?merge:

the name "row.names" or the number 0 specifies the row names.

Example:

R> de <- merge(d, e, by=0, all=TRUE)  # merge by row names (by=0 or by="row.names")
R> de[is.na(de)] <- 0 # replace NA values
R> de
Row.names a b c d e f g h i j k l m n o p q r s
1 1 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10 11 12 13 14 15 16 17 18 19
2 2 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 0 0 0 0 0 0 0 0 0
3 3 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0 21 22 23 24 25 26 27 28 29
t
1 20
2 0
3 30

Merge list of uneven dataframes by rownames

We create a row names column and then do the join. We loop through the list with map, create a row names column with rownames_to_column and reduce to a single dataset by doing a full_join by the row names and rename the column names if needed

library(tidyverse)
l %>%
map( ~ .x %>%
rownames_to_column('rn')) %>%
reduce(full_join, by = 'rn') %>%
rename_at(2:6, ~ names(l))
# rn V W X Y Z
#1 A 1 1 1 0 0
#2 B 1 0 1 NA 0
#3 C 1 NA 1 0 0
#4 D NA 0 NA 0 1

Or another option is to bind_rows and then spread

l %>%
map(rownames_to_column, 'rn') %>%
bind_rows(.id = 'grp') %>%
spread(grp, answer)
# rn V W X Y Z
#1 A 1 1 1 0 0
#2 B 1 0 1 NA 0
#3 C 1 NA 1 0 0
#4 D NA 0 NA 0 1

Combine/match/merge vectors by row names

The answers offered by Julius Vainora and achimneyswallow work well, but just to exactly obtain the desired output I want:

temp <- merge(Matrix_1, Matrix_2, by = "row.names", all = TRUE)
temp$Row.names <- factor(temp$Row.names, levels=Complete_names)
temp <- temp[order(temp$Row.names),]
rownames(temp) <- temp[,1]
Desired_output <- as.matrix(temp[,-1])

V1.x V1.y
D 1 NA
C 2 6
A NA 4
B 3 5

Combining multiple .csv files using row.names

We may do this in tidyverse

library(dplyr)
library(purrr)
map(count_lists, ~ .x %>%
rownames_to_column('rn')) %>%
reduce(full_join, by = 'rn') %>%
mutate(across(everything(), replace_na, 0))

how to merge or join data frame and keep the row names as well?

I guess you want to cbind the datasets keeping the rownames. An option using data.table is

library(data.table) #data.table_1.9.5
dt <- do.call(cbind,lapply(mget(paste0("df",1:3)),
as.data.table, keep.rownames=TRUE))
setnames(dt, seq(2,ncol(dt),by=2), rep('variable',3))
setnames(dt, seq(1,ncol(dt), by=2), paste0('row.names', 1:(ncol(dt)/2)))
head(dt,2)
# row.names1 variable row.names2 variable row.names3 variable
#1: 1 0 1 1 1 1
#2: 2 0 2 1 2 0


Related Topics



Leave a reply



Submit