Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'as.tibble' causes error in tibble 2.0.1 but not 1.4.2

Tags:

r

tibble

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

enter image description here

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?

like image 690
Jonno Bourne Avatar asked Feb 08 '19 12:02

Jonno Bourne


People also ask

How do you define Tibble?

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).

How do I create a new Tibble?

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.

What does Tibble :: Enframe () do?

enframe() converts a named vector to a tib- ble with a column of names and column of values.

What does Tibble function do in R?

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.


1 Answers

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.

like image 78
duckmayr Avatar answered Sep 28 '22 05:09

duckmayr