Letin
Letin

Reputation: 1275

rbind() function in R produces NA's in the merged dataframe

I need some help regarding the rbind function in R. I have the following 2 dataframes.

df1

        col1    col2    col3
row1        0       1       0
row2    txt1    txt2    txt3
row3    txtA    txtB    txtC
row4        51      93      83

df2

        col1    col2    col3
row5    0.732   0.345   0.532
row6    0.453   0.123   0.456
row7    0.656   0.987   0.321
row8    0.432   0.030   0.754

I want to merge these 2 dataframes, so I use rbind function to get the following:

        col1    col2    col3
row1        0       1       0
row2    txt1    txt2    txt3
row3    txtA    txtB    txtC
row4        51      93      83
row5    0.732   0.345   0.532
row6    0.453   0.123   0.456
row7    0.656   0.987   0.321
row8    0.432   0.030   0.754

However, that's not what I get. When I used merge <- rbind(df1,df2), I get

    col1    col2    col3
row1        0       1       0
row2    txt1    txt2    txt3
row3    txtA    txtB    txtC
row4        51      93      83
row5    <NA>    <NA>    <NA>
row6    <NA>    <NA>    <NA>
row7    <NA>    <NA>    <NA>
row8    <NA>    <NA>    <NA>

So, when I merge these 2 dataframes, I get NA values for the df2. Could anyone help me with getting this right?

Thanks in advance!

Upvotes: 2

Views: 4012

Answers (3)

akrun
akrun

Reputation: 887068

?rbindlist from library(data.table) seems to work with factor columns as well.

 df1 <- read.table(text='col1    col2    col3
 row1        0       1       0
 row2    txt1    txt2    txt3
 row3    txtA    txtB    txtC
 row4        51      93      83',header=T,stringsAsFactors=T)

 df2 <-  read.table(text='col1    col2    col3
 row5    0.732   0.345   0.532
 row6    0.453   0.123   0.456
 row7    0.656   0.987   0.321
 row8    0.432   0.030   0.754',header=T)

 library(data.table)
  rbindlist(list(df1,df2)) #returns factor columns
 #  col1  col2  col3
 #1:     0     1     0
 #2:  txt1  txt2  txt3
 #3:  txtA  txtB  txtC
 #4:    51    93    83
 #5: 0.732 0.345 0.532
 #6: 0.453 0.123 0.456
 #7: 0.656 0.987 0.321
 #8: 0.432  0.03 0.754

Upvotes: 1

user1981275
user1981275

Reputation: 13372

The problem is that one dataframe has only numeric values and the other one has not.

Here is a workaround:

> data.frame(t(data.frame(t(df1), t(df2))))
      col1  col2  col3
row1     0     1     0
row2  txt1  txt2  txt3
row3  txtA  txtB  txtC
row4    51    93    83
row5 0.732 0.345 0.532
row6 0.453 0.123 0.456
row7 0.656 0.987 0.321
row8 0.432 0.030 0.754

I am not sure how you read in the data, but you could look at the stringsAsFactors argument to for example read.table or data.frame. If you set stringsAsFactors to FALSE you can use rbind.

To make your example reproducible:

> df1 = read.table(header=T, stringsAsFactors=F, text='        col1    col2    col3
+ row1        0       1       0
+ row2    txt1    txt2    txt3
+ row3    txtA    txtB    txtC
+ row4        51      93      83')

> df2 = read.table(header=T, text='        col1    col2    col3
+ row5    0.732   0.345   0.532
+ row6    0.453   0.123   0.456
+ row7    0.656   0.987   0.321
+ row8    0.432   0.030   0.754')

> rbind(df1, df2)
      col1  col2  col3
row1     0     1     0
row2  txt1  txt2  txt3
row3  txtA  txtB  txtC
row4    51    93    83
row5 0.732 0.345 0.532
row6 0.453 0.123 0.456
row7 0.656 0.987 0.321
row8 0.432  0.03 0.754

Upvotes: 2

zx8754
zx8754

Reputation: 56149

Problem is df1 columns are factor, use as.is=TRUE when reading in the data.

Example:

#reproducible df1
df1 <- read.table(text="
col1    col2    col3
row1        0       1       0
row2    txt1    txt2    txt3
row3    txtA    txtB    txtC
row4        51      93      83",header=TRUE,as.is=TRUE)

#reproducible df2
df2 <- read.table(text="
col1    col2    col3
row5    0.732   0.345   0.532
row6    0.453   0.123   0.456
row7    0.656   0.987   0.321
row8    0.432   0.030   0.754",header=TRUE)

#result
rbind(df1,df2)

# col1  col2  col3
# row1     0     1     0
# row2  txt1  txt2  txt3
# row3  txtA  txtB  txtC
# row4    51    93    83
# row5 0.732 0.345 0.532
# row6 0.453 0.123 0.456
# row7 0.656 0.987 0.321
# row8 0.432  0.03 0.754

Upvotes: 2

Related Questions