R: bind_rows 由于未命名的参数 1 而失败
R: bind_rows fails because of unnamed argument 1
这已被多次以不同的形式询问,但这里是一个非常简单的例子。我过去曾多次使用过这个功能,但显然在最简单的情况下它失败得很惨。
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 创建
知道如何解决这个问题吗?
非常感谢!
它只是一个向量,?bind_rows
输入应该是一组数据帧
... - Data frames to combine.
一个选项是使用 map
遍历 list
,转换为 data.frame
并使用 _dfr
按行附加它们
library(purrr)
map_dfr(ll, as.data.frame.list)
或t
转置并转换为data.frame
map_dfr(ll, ~ as.data.frame(t(.x)))
或者这也可以是命名的vector
map_dfr(ll, ~ setNames(.x, seq_along(.x)))
或者使用 rbind
和 base R
中的 do.call
因为 rbind
有 matrix
和 data.frame
的方法
do.call(rbind, ll)
do.call(rbind.data.frame, ll)
这已被多次以不同的形式询问,但这里是一个非常简单的例子。我过去曾多次使用过这个功能,但显然在最简单的情况下它失败得很惨。
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 创建知道如何解决这个问题吗?
非常感谢!
它只是一个向量,?bind_rows
输入应该是一组数据帧
... - Data frames to combine.
一个选项是使用 map
遍历 list
,转换为 data.frame
并使用 _dfr
library(purrr)
map_dfr(ll, as.data.frame.list)
或t
转置并转换为data.frame
map_dfr(ll, ~ as.data.frame(t(.x)))
或者这也可以是命名的vector
map_dfr(ll, ~ setNames(.x, seq_along(.x)))
或者使用 rbind
和 base R
中的 do.call
因为 rbind
有 matrix
和 data.frame
do.call(rbind, ll)
do.call(rbind.data.frame, ll)