4

I try to handle with readr function. I mean, this is an example from readr github webpage:

read_csv("iris.csv", col_types = list(
  Sepal.Length = col_double(),
  Sepal.Width = col_double(),
  Petal.Length = col_double(),
  Petal.Width = col_double(),
  Species = col_factor(c("setosa", "versicolor", "virginica"))
))

Is there any chance to use within read_csv a function which can determine col_double for several columns at once e.g. grepl("Length|Witdh",col_names) = col_double()?

Thanks,

smci
  • 32,567
  • 20
  • 113
  • 146
Nicolabo
  • 1,337
  • 12
  • 30
  • This may not be helpful, but you don't have to specify every column type. Any you exclude will be read in the default format. So if the Length and Width columns are numbers, they'll be double automatically. – Josh Aug 07 '15 at 23:30
  • Yeah I know. I just use this code as an example. I have more than 20 columns in my csv.file and about 8 for which I have to change the class. Thus I do not want to do it for each that column `col_...()` command separately. – Nicolabo Aug 07 '15 at 23:42
  • 2
    @Nicolabo did you find a solution? – user2909302 Sep 17 '18 at 02:58
  • 1
    Does this answer your question? [r: read\_csv, cols(): Specify multiple column types at once](https://stackoverflow.com/questions/71364997/r-read-csv-cols-specify-multiple-column-types-at-once) – zephryl Mar 05 '22 at 21:43

1 Answers1

0

I've provided my answer that I posted on a similar thread to here. let me know if it helps.

Assign the columns names you want to a vector; eg

custom_col_logic<- c("logi_one","logi_two") 

custom_col_date<- c("date_one","date_two") 

Then use the map() function on each to apply the col_logic() and col_date() in to separate arguments. Then assign the column names to each of the arguments.

#assign elments col_logic or col_date
type_logical <-map(custom_col_logic,~col_logic())

type_date <-map(custom_col_date,~col_date())

#now assign the column names to this
names(type_logtical)<-custom_col_logic

names(type_date) <-custom_col_date

Here is the trick, you then need to use the as.col_spec() argument to turn these two vectors into col_spec class.

type_logical<- as.col_spec(type_logical)
type_date <- as.col_spec(type_date)

Lastly assign a new variable to cols() and then add to that variable the above custom cols #assign new varibale to class cols

custom_col_type <- cols()

#assign the variables from before to this new variable's cols argument

custom_col_type$cols <- c(type_logical,type_date)

Then you are done! now you can use that as a direct argument in the col_type argument in read_csv()

Thanks!

If you found this helpful, please vote or mark it as the final answer

alejandro_hagan
  • 843
  • 2
  • 13