Closed
Description
I was writing a tidier function with as_data_frame
to convert a matrix generated by a model comparison function. The conversion appeared to work until tried to mutate
the tibble. Then I got the error Error: matrix as column is not supported
. I was able to trace the problem to how the columns in tibble retained their "matrix" class.
Here is an example using stats::poly
, which also produces a subclassed matrix.
I decided to file an issue because this behavior is unexpected and because as.data.frame
does work as expected.
library("dplyr", warn.conflicts = FALSE)
#> Warning: package 'dplyr' was built under R version 3.3.1
library("tibble")
poly(1:6, 3) %>% str
#> poly [1:6, 1:3] -0.598 -0.359 -0.12 0.12 0.359 ...
#> - attr(*, "dimnames")=List of 2
#> ..$ : NULL
#> ..$ : chr [1:3] "1" "2" "3"
#> - attr(*, "coefs")=List of 2
#> ..$ alpha: num [1:3] 3.5 3.5 3.5
#> ..$ norm2: num [1:5] 1 6 17.5 37.3 64.8
#> - attr(*, "degree")= int [1:3] 1 2 3
#> - attr(*, "class")= chr [1:2] "poly" "matrix"
poly(1:6, 3) %>% as_data_frame %>% mutate(Condition = "Test")
#> Error in eval(expr, envir, enclos): matrix as column is not supported
poly(1:6, 3) %>% as_data_frame %>% str
#> Classes 'tbl_df', 'tbl' and 'data.frame': 6 obs. of 3 variables:
#> $ 1:Classes 'poly', 'matrix' atomic [1:6] -0.598 -0.359 -0.12 0.12 0.359 ...
#> .. ..- attr(*, "coefs")=List of 2
#> .. .. ..$ alpha: num [1:3] 3.5 3.5 3.5
#> .. .. ..$ norm2: num [1:5] 1 6 17.5 37.3 64.8
#> .. ..- attr(*, "degree")= int [1:3] 1 2 3
#> $ 2:Classes 'poly', 'matrix' atomic [1:6] 0.546 -0.109 -0.436 -0.436 -0.109 ...
#> .. ..- attr(*, "coefs")=List of 2
#> .. .. ..$ alpha: num [1:3] 3.5 3.5 3.5
#> .. .. ..$ norm2: num [1:5] 1 6 17.5 37.3 64.8
#> .. ..- attr(*, "degree")= int [1:3] 1 2 3
#> $ 3:Classes 'poly', 'matrix' atomic [1:6] -0.373 0.522 0.298 -0.298 -0.522 ...
#> .. ..- attr(*, "coefs")=List of 2
#> .. .. ..$ alpha: num [1:3] 3.5 3.5 3.5
#> .. .. ..$ norm2: num [1:5] 1 6 17.5 37.3 64.8
#> .. ..- attr(*, "degree")= int [1:3] 1 2 3
# workaround
poly(1:6, 3) %>% as.data.frame %>% as_data_frame %>% mutate(Condition = "Test")
#> # A tibble: 6 x 4
#> 1 2 3 Condition
#> <dbl> <dbl> <dbl> <chr>
#> 1 -0.5976143 0.5455447 -0.3726780 Test
#> 2 -0.3585686 -0.1091089 0.5217492 Test
#> 3 -0.1195229 -0.4364358 0.2981424 Test
#> 4 0.1195229 -0.4364358 -0.2981424 Test
#> 5 0.3585686 -0.1091089 -0.5217492 Test
#> 6 0.5976143 0.5455447 0.3726780 Test
devtools::session_info()
#> Session info --------------------------------------------------------------
#> setting value
#> version R version 3.3.0 (2016-05-03)
#> system x86_64, mingw32
#> ui RTerm
#> language (EN)
#> collate English_United States.1252
#> tz America/Chicago
#> date 2016-06-28
#> Packages ------------------------------------------------------------------
#> package * version date source
#> assertthat 0.1 2013-12-06 CRAN (R 3.0.2)
#> DBI 0.4-1 2016-05-08 CRAN (R 3.2.5)
#> devtools 1.12.0 2016-06-24 CRAN (R 3.3.1)
#> digest 0.6.9 2016-01-08 CRAN (R 3.2.2)
#> dplyr * 0.5.0 2016-06-24 CRAN (R 3.3.1)
#> evaluate 0.9 2016-04-29 CRAN (R 3.2.5)
#> formatR 1.4 2016-05-09 CRAN (R 3.2.3)
#> htmltools 0.3.5 2016-03-21 CRAN (R 3.2.4)
#> knitr 1.13 2016-05-09 CRAN (R 3.2.3)
#> lazyeval 0.2.0 2016-06-12 CRAN (R 3.3.0)
#> magrittr 1.5 2014-11-22 CRAN (R 3.1.2)
#> memoise 1.0.0 2016-01-29 CRAN (R 3.2.3)
#> R6 2.1.2 2016-01-26 CRAN (R 3.2.3)
#> Rcpp 0.12.5 2016-05-14 CRAN (R 3.2.5)
#> rmarkdown 0.9.6 2016-05-01 CRAN (R 3.2.3)
#> stringi 1.1.1 2016-05-27 CRAN (R 3.2.5)
#> stringr 1.0.0 2015-04-30 CRAN (R 3.2.0)
#> tibble * 1.0-12 2016-06-28 Github (hadley/tibble@1e5b140)
#> withr 1.0.2 2016-06-20 CRAN (R 3.3.1)
#> yaml 2.1.13 2014-06-12 CRAN (R 3.1.0)