Skip to content

write_csv only saves first column of matrix-columns in tibbles #1171

@Aariq

Description

@Aariq

Just like list-columns in tibbles, you can create a tibble with a column that itself is a matrix. Trying to write a tibble with a list-column to a .csv throws an error, but a matrix-column does not. Only the first column of the matrix is saved though. This should probably either throw an error or save all the columns of the matrix-column.

library(tibble)
library(readr)
my_matrix <- matrix(rnorm(100), nrow = 10)
tbl <- tibble(x = letters[1:10], mat_col = my_matrix)
tbl
#> # A tibble: 10 x 2
#>    x     mat_col[,1]    [,2]   [,3]    [,4]    [,5]   [,6]    [,7]    [,8]
#>    <chr>       <dbl>   <dbl>  <dbl>   <dbl>   <dbl>  <dbl>   <dbl>   <dbl>
#>  1 a         -0.233   1.55   -0.450 -0.0861 -1.21   -1.67  -0.594   0.125 
#>  2 b          0.216  -0.393  -2.38  -0.959  -0.357  -0.130 -0.635  -1.21  
#>  3 c          0.436  -0.0963  1.17   0.813  -0.607  -1.30  -0.129   0.708 
#>  4 d         -0.0352 -1.08   -1.08  -1.14    0.824  -2.17   0.754  -0.432 
#>  5 e          1.04   -0.614  -1.35  -2.04   -1.30   -0.628 -0.782  -0.0613
#>  6 f         -1.16    0.282  -0.702 -1.20   -0.869   0.174  0.0553  1.10  
#>  7 g          0.812   0.359  -0.409  3.07    0.258  -1.03   0.478   0.882 
#>  8 h         -0.573  -0.547  -0.327  0.484   0.0848  0.470 -1.35    0.226 
#>  9 i         -0.113   0.247   1.54  -0.234  -1.25    1.01  -0.194  -0.357 
#> 10 j         -0.0136  0.599  -0.464 -0.513   1.73    0.494  0.696  -0.698 
#> # … with 2 more variables: [,9] <dbl>, [,10] <dbl>
write_csv(tbl, "test.csv")
read_csv("test.csv")
#> 
#> ── Column specification ────────────────────────────────────────────────────────
#> cols(
#>   x = col_character(),
#>   mat_col = col_double()
#> )
#> # A tibble: 10 x 2
#>    x     mat_col
#>    <chr>   <dbl>
#>  1 a     -0.233 
#>  2 b      0.216 
#>  3 c      0.436 
#>  4 d     -0.0352
#>  5 e      1.04  
#>  6 f     -1.16  
#>  7 g      0.812 
#>  8 h     -0.573 
#>  9 i     -0.113 
#> 10 j     -0.0136

Created on 2021-01-11 by the reprex package (v0.3.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions