I have written a function part of which converts a matrix to a tibble. This works without issues in tibble 1.4.2 but causes an error in 2.0.1.
The code that causes the error is as follows
library(tibble)
library(magrittr)
testmerge <- matrix( data = NA, ncol = 6 + 1, nrow = 0) %>%
as.tibble
The Error message is below
I can solve the problem by doing the following
testmerge <- matrix( data = NA, ncol = 6 + 1, nrow = 0) %>%
as.data.frame() %>%
as_tibble
But this seems a bit long winded.
What is happening that has caused this change? And how can I easily end up with a tibble of just empty columns?
Tibbles are data. frames that are lazy and surly: they do less (i.e. they don't change variable names or types, and don't do partial matching) and complain more (e.g. when a variable does not exist).
You can create a new tibble from individual vectors with tibble() . tibble() will automatically recycle inputs of length 1, and allows you to refer to variables that you just created, as shown below.
enframe() converts a named vector to a tib- ble with a column of names and column of values.
tibble() builds columns sequentially. When defining a column, you can refer to columns created earlier in the call. Only columns of length one are recycled. If a column evaluates to a data frame or tibble, it is nested or spliced.
You need to specify .name_repair
; see ?as_tibble
:
library(tibble)
library(magrittr)
sessionInfo()
#> R version 3.5.2 (2018-12-20)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 18.04.1 LTS
#>
#> Matrix products: default
#> BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
#>
#> locale:
#> [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
#> [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
#> [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
#> [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
#> [9] LC_ADDRESS=C LC_TELEPHONE=C
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] magrittr_1.5 tibble_2.0.1
#>
#> loaded via a namespace (and not attached):
#> [1] Rcpp_1.0.0 digest_0.6.18 crayon_1.3.4 rprojroot_1.3-2
#> [5] backports_1.1.2 evaluate_0.11 pillar_1.3.1 rlang_0.3.1
#> [9] stringi_1.2.4 rmarkdown_1.10 tools_3.5.2 stringr_1.3.1
#> [13] yaml_2.2.0 compiler_3.5.2 pkgconfig_2.0.2 htmltools_0.3.6
#> [17] knitr_1.20
Your code worked just fine for me with tibble_1.4.2
, as you describe, but after upgrading to tibble_2.0.1
, I end up with the same error you had, but with a slightly more informative message that included the sentence Use .name_repair to specify repair.
:
testmerge <- matrix( data = NA, ncol = 6 + 1, nrow = 0) %>%
as_tibble()
#> Error: Columns 1, 2, 3, 4, 5, … (and 2 more) must be named.
#> Use .name_repair to specify repair.
testmerge <- matrix( data = NA, ncol = 6 + 1, nrow = 0) %>%
as_tibble(.name_repair = "unique")
#> New names:
#> * `` -> `..1`
#> * `` -> `..2`
#> * `` -> `..3`
#> * `` -> `..4`
#> * `` -> `..5`
#> * … and 2 more
testmerge
#> # A tibble: 0 x 7
#> # … with 7 variables: ..1 <lgl>, ..2 <lgl>, ..3 <lgl>, ..4 <lgl>,
#> # ..5 <lgl>, ..6 <lgl>, ..7 <lgl>
Update, in the comments, @NelsonGon links to a GitHub issue, the discussion of which seems to have led to this new behavior.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With