Using vroom to read in Date column and all other columns as double in R

615 views Asked by At

I have csv files with over 10000 variables in them. I want to use vroom to read them in, and want to identify column 1 as a date, column 2 as character, columns 3 and 4 as integer, and all the rest of the columns as double. How do I do this?

My code looks like this, but fails.

data81 <- vroom(fname_1981_2010, col_types = c(Date = col_date(), MonthDay = col_character(), doy = col_integer(), (.) = col_double()))

What is the syntax to tell vroom to read in the rest of the columns as double (replacing the (.) in my code? Thank you.

1

There are 1 answers

0
stefan On BEST ANSWER

The could be achieved via the .default argument of cols():

library(vroom)

set.seed(42)

d <- data.frame(
  date = sample(seq(as.Date('1999/01/01'), as.Date('2000/01/01'), by="day"), 12),
  MonthDay = sample(LETTERS[1:7], 12, replace = TRUE),
  doy = 1:12,
  col4 = runif(12),
  col5 = runif(12),
  col6 = runif(12)
)

fname_1981_2010 <- "fname_1981_2010.csv"

write.csv(d, fname_1981_2010, row.names = FALSE)

vroom(fname_1981_2010, col_types = cols(date = col_date(), MonthDay = col_character(), 
      doy = col_integer(), .default = col_double()))
#> # A tibble: 12 x 6
#>    date       MonthDay   doy    col4   col5   col6
#>    <date>     <chr>    <int>   <dbl>  <dbl>  <dbl>
#>  1 1999-02-18 D            1 0.514   0.208  0.619 
#>  2 1999-11-17 A            2 0.390   0.907  0.333 
#>  3 1999-06-02 E            3 0.906   0.612  0.347 
#>  4 1999-03-15 F            4 0.447   0.380  0.398 
#>  5 1999-08-16 D            5 0.836   0.436  0.785 
#>  6 1999-05-26 B            6 0.738   0.0374 0.0389
#>  7 1999-05-02 B            7 0.811   0.974  0.749 
#>  8 2000-01-01 G            8 0.388   0.432  0.677 
#>  9 1999-05-08 C            9 0.685   0.958  0.171 
#> 10 1999-10-30 A           10 0.00395 0.888  0.261 
#> 11 1999-01-24 A           11 0.833   0.640  0.514 
#> 12 1999-11-23 C           12 0.00733 0.971  0.676