Read a CSV file into a typed table
Read hourly weather at the three New York City airports in 2013, from nycflights13’s weather table in the PHPstats datasets, straight from its URL. One column’s type is declared, and the rest are guessed:
use Phibble\DataFrame;use function Phibble\Expr\{col, is_na, sum};$weather = DataFrame::fromCsv( 'https://raw.githubusercontent.com/phpstats/datasets/' .'main/csv/weather.csv', types: ['origin' => 'factor'],);echo $weather->glimpse(), "\n";echo implode(', ', $weather->levels('origin')), "\n\n";echo $weather->summarize([ 'temp' => sum(is_na(col('temp'))), 'wind_gust' => sum(is_na(col('wind_gust'))), 'pressure' => sum(is_na(col('pressure'))),]), "\n";Rows: 26,115
Columns: 16
$ rownames <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, …
$ origin <fct> EWR, EWR, EWR, EWR, EWR, EWR, EWR, EWR, EWR, EWR, EWR, EWR,…
$ year <int> 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013,…
$ month <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
$ day <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
$ hour <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, …
$ temp <dbl> 39.02, 39.02, 39.02, 39.92, 39.02, 37.94, 39.02, 39.92, 39.…
$ dewp <dbl> 26.06, 26.96, 28.04, 28.04, 28.04, 28.04, 28.04, 28.04, 28.…
$ humid <dbl> 59.37, 61.63, 64.43, 62.21, 64.43, 67.21, 64.43, 62.21, 62.…
$ wind_dir <int> 270, 250, 240, 250, 260, 240, 240, 250, 260, 260, 260, 330,…
$ wind_speed <dbl> 10.35702, 8.05546, 11.50780, 12.65858, 12.65858, 11.50780, …
$ wind_gust <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 20.…
$ precip <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
$ pressure <dbl> 1012.0, 1012.3, 1012.5, 1012.2, 1011.9, 1012.4, 1012.2, 101…
$ visib <dbl> 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,…
$ time_hour <dttm> 2013-01-01 06:00:00, 2013-01-01 07:00:00, 2013-01-01 08:00…
EWR, JFK, LGA
# A tibble: 1 × 3
temp wind_gust pressure
<int> <int> <int>
1 1 20778 2729
- The 26,115 rows give 16 columns.
rownamesholds the row names the table had in R. - Whole numbers, such as
year,hour, andwind_dir, become int, where readr’sread_csv()gives double. Decimals become double. time_hourholds ISO 8601 text such as2013-01-01T06:00:00Z, which is guessed as a datetime in UTC.typesdeclaresorigina factor, with the levels EWR, JFK, and LGA in sorted order. Without it,originwould be a string.- Empty fields become NA: one hour has no temperature, 20,778 of 26,115 have no wind gust, and 2,729 have no pressure.
- Types are guessed from up to 1,000 sampled rows. Declare a type when those rows could mislead the guess.
Runnable script: read-a-csv.php. It reads the file over HTTPS, so allow_url_fopen must be on.
Reference: Reading and writing data · Types.