[R] readr to generate tibble from a character matrix

Ben Tupper btupper at bigelow.org
Thu Apr 6 16:40:50 CEST 2017


Hello,

I have a workflow yields a character matrix that I convert to a tibble. Here is a simple example.

library(tibble)
library(readr)

m <- matrix(c(letters[1:12], 1:4, (11:14 + 0.2)), ncol = 5)
colnames(m) <- LETTERS[1:5]

x <- as_tibble(m)

# # A tibble: 4 × 5
#       A     B     C     D     E
#   <chr> <chr> <chr> <chr> <chr>
# 1     a     e     i     1  11.2
# 2     b     f     j     2  12.2
# 3     c     g     k     3  13.2
# 4     d     h     l     4  14.2

The workflow output columns can be a mix of a known set column outputs.  Some of the columns really should be converted to non-character types before I proceed.  Right now I explictly set the column classes with something like this...

mode(x[['D']]) <- 'integer'
mode(x[['E']]) <- 'numeric'

# # A tibble: 4 × 5
#       A     B     C     D     E
#   <chr> <chr> <chr> <int> <dbl>
# 1     a     e     i     1  11.2
# 2     b     f     j     2  12.2
# 3     c     g     k     3  13.2
# 4     d     h     l     4  14.2


I wonder if there is a way to use the read_* functions in the readr package to read the character matrix into a tibble directly which would leverage readr's excellent column class guessing. I can see in the vignette ( https://cran.r-project.org/web/packages/readr/vignettes/readr.html ) that I'm not too far off in thinking this could be done (step 1 tantalizingly says 'The flat file is parsed into a rectangular matrix of strings.')  

I know that I could either write the matrix to a file or paste it all into a character vector and then use read_* functions, but I confess I am looking for a straighter path by simply passing the matrix to a function like readr::read_matrix() or the like.

Thanks!
Ben

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544
http://www.bigelow.org



More information about the R-help mailing list