R readr single col_types

Is it possible in a package readr

to read data and specify the same data type for all columns? Likewise base::read.table

with colClasses = "character"

or using an argument as.is

.

If the task, data headers, file encoding, etc. not predefined, I prefer to write my loaders without changing data types and then process the schema later. Always be open to guesswork about how other people think about things.

+3


source to share


2 answers


Converting my comment to answer. No, it is not built in (at the moment), the documentation col_types

is quite clear regarding its capabilities, this is not one of them. Given the way col_types

, it will probably need a new argument to implement this, since the function is to use "short" to limit the number of columns read col_types

.

However, you can write a shell:



read_table_asis = function(...) {
    n_cols = ncol(read_table(..., n_max = 1))
    read_table(..., col_types = paste(rep("c", n_cols), collapse = ""))
}

      

+3


source


As of readr 0.2.2, we can do something like this to read the csv with all columns as a character:



read_csv("path/to/file",col_types = cols(.default = col_character()))

      

+6


source







All Articles