2014-06-23 44 views
1

我需要關於R的rbind功能,我有以下2個dataframes一些幫助。rbind()函數產生NA在合併後的數據幀

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 

我要合併這2個dataframes,所以我用rbind功能得到以下:

 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 

然而,這並不是什麼我明白了。當我用 合併< - rbind(DF1,DF2),我得到

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> 

所以,當我合併這2個dataframes,我得到NA爲DF2值。任何人都可以幫助我解決這個問題嗎?

在此先感謝!

+1

總是提供[一個可重複的例子](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)。 – Thomas

回答

2

的問題是,一個數據幀只有數值和另外一個也沒有。

這裏是一個解決辦法:

> 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 

我不知道你的數據是如何讀,但你可以看看stringsAsFactors參數例如read.tabledata.frame。如果設置stringsAsFactorsFALSE可以 使用rbind

爲了讓您的例子重複性:

> 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 
+0

謝謝!它現在有效。 – Letin

+0

+1好用的技巧't()' – zx8754

2

問題是df1列因素,在數據讀取時使用as.is=TRUE

實施例:

#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 
+0

感謝您的解釋。這也有幫助! – Letin

1

?rbindlist從庫(data.table)似乎與因子列於正常工作。

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