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