【问题标题】:rbind() function in R produces NA's in the merged dataframeR 中的 rbind() 函数在合并的数据帧中产生 NA
【发布时间】:2014-08-13 14:09:14
【问题描述】:

我需要一些关于 R 中 rbind 函数的帮助。我有以下 2 个数据帧。

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

我想合并这两个数据帧,所以我使用 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

但是,这不是我得到的。当我使用 合并

    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>

所以,当我合并这两个数据帧时,我得到了 df2 的 NA 值。任何人都可以帮我解决这个问题吗?

提前致谢!

【问题讨论】:

标签: r merge rbind


【解决方案1】:

问题是一个数据框只有数值,而另一个没有。

这里有一个解决方法:

> 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。如果您将stringsAsFactors 设置为FALSE,您可以 使用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

【讨论】:

    【解决方案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
    

    【讨论】:

    • 感谢您的解释。这也有帮助!
    【解决方案3】:

    library(data.table) 中的?rbindlist 似乎也适用于因子列。

     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
    

    【讨论】:

      猜你喜欢
      • 2019-11-05
      • 2013-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-04
      • 1970-01-01
      • 2023-03-05
      • 2020-10-26
      相关资源
      最近更新 更多