How do you rbind two data frames with a different number of columns without loosing rownames?
1
0
Entering edit mode
5.1 years ago
anamaria ▴ 220

Hi,

I have two data frames:

> head(df2)
                ILMN_1658176 ILMN_1794782 ILMN_2329927 ILMN_2338268
DwoC_2318_norm      4.895369     7.354273     5.788415     4.732194
DwoC_3395_norm      4.769932     6.718184     5.338058     4.680381
DwoC_6154_norm      4.944610     7.001293     5.351585     4.456892
DwoC_16362_norm     4.637149     5.774219     4.720364     4.687178
DwoC_18296_norm     4.738705     6.941641     5.472976     4.843630
DwoC_21183_norm     4.883878     7.481384     5.755646     4.505568

> head(df1)
               ILMN_1658176     ILMN_2329927     ILMN_1794782     ILMN_2338268
GTEX-1122O 7.06228526333386 7.06228526333386 7.06228526333386 3.57044703372789
GTEX-11EM3 6.05973723132698 6.05973723132698 6.05973723132698  3.4268959808612
GTEX-11EMC 6.38938647129766 6.38938647129766 6.38938647129766 3.47843984281137
GTEX-11EQ9 6.24361469623367 6.24361469623367 6.24361469623367 3.51276310600448
GTEX-11I78 4.56890242439573 4.56890242439573 4.56890242439573 3.48287352055029
GTEX-11OC5 7.26717305438927 7.26717305438927 7.26717305438927 3.34892592791344

I tried to do rbind via:

library(data.table)
a=rbindlist(list(df1, df2), fill = TRUE)

but in my data frame "a" rownames are not present. How do I do rbind and keep them?

Thanks Ana

R • 8.2k views
ADD COMMENT
2
Entering edit mode
5.1 years ago
Ram 44k

This is a pure R question, but here we go: use tidyverse packages

library(dplyr)
library(tibble)

res <- df1 %>% rownames_to_column('rnames') %>% 
               bind_rows(df2 %>% rownames_to_column('rnames')) %>% 
               column_to_rownames('rnames');

res

                    ILMN_1658176 ILMN_1794782 ILMN_2329927 ILMN_2338268
DwoC_2318_norm      4.895369     7.354273     5.788415     4.732194
DwoC_3395_norm      4.769932     6.718184     5.338058     4.680381
DwoC_6154_norm      4.944610     7.001293     5.351585     4.456892
DwoC_16362_norm     4.637149     5.774219     4.720364     4.687178
DwoC_18296_norm     4.738705     6.941641     5.472976     4.843630
DwoC_21183_norm     4.883878     7.481384     5.755646     4.505568
GTEX-1122O          7.062285     7.062285     7.062285     3.570447
GTEX-11EM3          6.059737     6.059737     6.059737     3.426896
GTEX-11EMC          6.389386     6.389386     6.389386     3.478440
GTEX-11EQ9          6.243615     6.243615     6.243615     3.512763
GTEX-11I78          4.568902     4.568902     4.568902     3.482874
GTEX-11OC5          7.267173     7.267173     7.267173     3.348926

Or you can use base R to create new columns from the rownames and move the column value back to rownames once done.

df1$rnames <- rownames(df1);
df2$rnames <- rownames(df2);
res <- rbind(df1,df2);
rownames(res) <- res$rnames;
res <- res[, !(colnames(res) %in% 'rnames')]
ADD COMMENT
0
Entering edit mode

Thank you so much, this worked great!

ADD REPLY

Login before adding your answer.

Traffic: 2648 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6