Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R readr single col_types

Tags:

r

dplyr

readr

Is it possible within readr package to read in data and specify a single data type across all columns? Similar to base::read.table with colClasses = "character" or using the as.is argument.

Unless the task, data headers, file encoding, etc are well defined ahead of analysis I prefer to write my loaders without changing the datatypes and then process the schema later downstream. Always open to suggestion on how other folks think about things.

like image 859
gbartusk Avatar asked Dec 20 '22 01:12

gbartusk


2 Answers

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

read_csv("path/to/file",col_types = cols(.default = col_character()))
like image 150
joran Avatar answered Jan 12 '23 07:01

joran


Converting my comment to answer. No, this isn't built-in (at this point), the documentation of the col_types is quite clear about its capabilities, this isn't one of them. Given the way col_types works, implementing this would probably require a brand new argument since a feature is that a "short" col_types will be used to restrict the number of columns read.

However, you could write a wrapper:

read_table_asis = function(...) {
    n_cols = ncol(read_table(..., n_max = 1))
    read_table(..., col_types = paste(rep("c", n_cols), collapse = ""))
}
like image 35
Gregor Thomas Avatar answered Jan 12 '23 08:01

Gregor Thomas