【问题标题】:R: bind_rows fails because of unnamed argument 1R:bind_rows 由于未命名的参数 1 而失败
【发布时间】:2021-06-06 14:56:23
【问题描述】:

这已经以不同的形式被问过好几次了,但这里有一个非常简单的例子。我过去曾多次使用过这个功能,但显然在最简单的情况下它失败了。

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(purrr)


bind_df_list <- function(ll){

    res <- map_df(ll, bind_rows)
    return(res)

    
}



ll<-list(seq(4), seq(4), seq(4))

dd<-bind_df_list(ll)
#> Error: Argument 1 must have names.

print(sessionInfo())
#> R version 4.0.4 (2021-02-15)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Debian GNU/Linux 10 (buster)
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
#> LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.3.5.so
#> 
#> locale:
#>  [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_GB.UTF-8        LC_COLLATE=en_GB.UTF-8    
#>  [5] LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
#>  [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] purrr_0.3.4 dplyr_1.0.4
#> 
#> loaded via a namespace (and not attached):
#>  [1] knitr_1.31        magrittr_2.0.1    tidyselect_1.1.0  R6_2.5.0         
#>  [5] rlang_0.4.10      stringr_1.4.0     styler_1.3.2      highr_0.8        
#>  [9] tools_4.0.4       xfun_0.21         DBI_1.1.1         htmltools_0.5.1.1
#> [13] ellipsis_0.3.1    assertthat_0.2.1  yaml_2.2.1        digest_0.6.27    
#> [17] tibble_3.0.6      lifecycle_1.0.0   crayon_1.4.1      vctrs_0.3.6      
#> [21] fs_1.5.0          glue_1.4.2        evaluate_0.14     rmarkdown_2.6    
#> [25] reprex_1.0.0      stringi_1.5.3     compiler_4.0.4    pillar_1.4.7     
#> [29] generics_0.1.0    backports_1.2.1   pkgconfig_2.0.3

reprex package (v1.0.0) 于 2021-03-08 创建

知道如何解决这个问题吗?

非常感谢!

【问题讨论】:

    标签: r dplyr purrr


    【解决方案1】:

    它只是一个向量,?bind_rows 输入应该是一组数据帧

    ... - 要组合的数据框。

    一种选择是循环使用list,使用map,转换为data.frame,然后使用_dfr逐行附加它们

    library(purrr)
    map_dfr(ll, as.data.frame.list)
    

    transpose 并转换为data.frame

    map_dfr(ll, ~ as.data.frame(t(.x)))
    

    或者这也可以是一个命名的vector

    map_dfr(ll, ~ setNames(.x, seq_along(.x)))
    

    或者使用 rbinddo.call from base R as rbindmatrixdata.frame 的方法

    do.call(rbind, ll)
    do.call(rbind.data.frame, ll)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多