larry77
larry77

Reputation: 1533

R: bind_rows fails because of unnamed argument 1

This has been asked several times in different forms, but here is a very simple example. I have used this function several times in the past, but apparently it fails miserably in the simplest case.

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

Created on 2021-03-08 by the reprex package (v1.0.0)

Any idea about how to fix this?

Many thanks!

Upvotes: 1

Views: 118

Answers (1)

akrun
akrun

Reputation: 887048

it is just a vector and ?bind_rows input should be a set of dataframes

... - Data frames to combine.

One option is to loop over the list, with map, convert to data.frame and append them by rows with _dfr

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

Or transpose and convert to data.frame

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

Or this can also be a named vector

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

Or use rbind with do.call from base R as rbind have methods for matrix and data.frame

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

Upvotes: 3

Related Questions