ropensci / rnoaa

R interface to many NOAA data APIs
https://docs.ropensci.org/rnoaa
Other
328 stars 85 forks source link

Specify column types returned by lcd #398

Closed mps9506 closed 3 years ago

mps9506 commented 3 years ago

Description

lcd() returns consistent types for each column. New internal function, lcd_columns() returns named character vector (accessible and changable by users) for use by a new argument, col_types, in lcd(). This is ultimately used by safe_read_csv() and the colClasses argument in data.table::fread(). This is slightly more complicated than the initial code used to ensure types are returned consistently. However, this allows users to specify the type if required or if I specified something incorrectly as a default. I think having the types stored in lcd_columns() will make updating in the future easier if new variables are added or changed in the future. fread will revert the column type to a lower type (with message) if an incompatible type is specified.

Related Issue

closes #396

Example

x.1 <- lcd(station = "74746003904", year = 2010)
x.1[c(1:6)]

# A tibble: 11,154 x 6
   station     date                latitude longitude elevation name                                   
   <chr>       <dttm>                 <dbl>     <dbl>     <dbl> <chr>                                  
 1 74746003904 2010-01-01 00:53:00     30.6     -96.4        93 COLLEGE STATION EASTERWOOD FIELD, TX US
 2 74746003904 2010-01-01 01:24:00     30.6     -96.4        93 COLLEGE STATION EASTERWOOD FIELD, TX US
 3 74746003904 2010-01-01 01:51:00     30.6     -96.4        93 COLLEGE STATION EASTERWOOD FIELD, TX US
 4 74746003904 2010-01-01 01:53:00     30.6     -96.4        93 COLLEGE STATION EASTERWOOD FIELD, TX US
 5 74746003904 2010-01-01 02:35:00     30.6     -96.4        93 COLLEGE STATION EASTERWOOD FIELD, TX US
 6 74746003904 2010-01-01 02:53:00     30.6     -96.4        93 COLLEGE STATION EASTERWOOD FIELD, TX US
 7 74746003904 2010-01-01 03:53:00     30.6     -96.4        93 COLLEGE STATION EASTERWOOD FIELD, TX US
 8 74746003904 2010-01-01 04:53:00     30.6     -96.4        93 COLLEGE STATION EASTERWOOD FIELD, TX US
 9 74746003904 2010-01-01 05:27:00     30.6     -96.4        93 COLLEGE STATION EASTERWOOD FIELD, TX US
10 74746003904 2010-01-01 05:53:00     30.6     -96.4        93 COLLEGE STATION EASTERWOOD FIELD, TX US
# ... with 11,144 more rows

col_types <- lcd_columns(STATION = "numeric")
x.2 <- lcd(station = "74746003904", year = 2010, col_types = col_types)
x.2[c(1:6)]

# A tibble: 11,154 x 6
       station date                latitude longitude elevation name                                   
         <dbl> <dttm>                 <dbl>     <dbl>     <dbl> <chr>                                  
 1 74746003904 2010-01-01 00:53:00     30.6     -96.4        93 COLLEGE STATION EASTERWOOD FIELD, TX US
 2 74746003904 2010-01-01 01:24:00     30.6     -96.4        93 COLLEGE STATION EASTERWOOD FIELD, TX US
 3 74746003904 2010-01-01 01:51:00     30.6     -96.4        93 COLLEGE STATION EASTERWOOD FIELD, TX US
 4 74746003904 2010-01-01 01:53:00     30.6     -96.4        93 COLLEGE STATION EASTERWOOD FIELD, TX US
 5 74746003904 2010-01-01 02:35:00     30.6     -96.4        93 COLLEGE STATION EASTERWOOD FIELD, TX US
 6 74746003904 2010-01-01 02:53:00     30.6     -96.4        93 COLLEGE STATION EASTERWOOD FIELD, TX US
 7 74746003904 2010-01-01 03:53:00     30.6     -96.4        93 COLLEGE STATION EASTERWOOD FIELD, TX US
 8 74746003904 2010-01-01 04:53:00     30.6     -96.4        93 COLLEGE STATION EASTERWOOD FIELD, TX US
 9 74746003904 2010-01-01 05:27:00     30.6     -96.4        93 COLLEGE STATION EASTERWOOD FIELD, TX US
10 74746003904 2010-01-01 05:53:00     30.6     -96.4        93 COLLEGE STATION EASTERWOOD FIELD, TX US
# ... with 11,144 more rows