Read CSV Data in R

Your goal

You need to read CSV data into R.

Step-by-step tutorial

We'll do this using the read_csv function from the readr library, which is part of the core tidyverse.

Careful...

Note that read_csv() is not the same as read.csv() from the core R distribution. read_csv() returns a tibble, whereas read.csv() returns a data.frame.

Here's the code:

> library(readr)
> read_csv("https://raw.githubusercontent.com/williewheeler/datasets/main/precip-central-park.csv")
Parsed with column specification:
cols(
  YEAR = col_double(),
  JAN = col_double(),
  FEB = col_double(),
  MAR = col_double(),
  APR = col_double(),
  MAY = col_double(),
  JUN = col_double(),
  JUL = col_double(),
  AUG = col_double(),
  SEP = col_double(),
  OCT = col_double(),
  NOV = col_double(),
  DEC = col_double(),
  ANNUAL = col_double()
)
# A tibble: 151 x 14
    YEAR   JAN   FEB   MAR   APR   MAY   JUN   JUL   AUG   SEP   OCT   NOV   DEC
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1  1869  2.53  6.87  4.61  1.39  4.15  4.4   3.2   1.76  2.81  6.48  2.03  5.02
 2  1870  4.41  2.83  3.33  5.11  1.83  2.82  3.76  3.07  2.52  4.97  2.42  2.18
 3  1871  2.07  2.72  5.54  3.03  4.04  7.05  5.57  5.6   2.34  7.5   3.56  2.24
 4  1872  1.88  1.29  3.74  2.29  2.68  2.93  7.83  6.29  2.95  3.35  4.08  3.18
 5  1873  5.34  3.8   2.09  4.16  3.69  1.28  4.61  9.56  3.14  2.73  4.63  2.96
 6  1874  5.33  2.04  2.12  8.77  2.24  2.78  5.06  2.43  8.24  1.7   2.3   2.82
 7  1875  3.17  2.62  3.48  3.08  1.33  2.72  4.89  8.97  1.89  2.85  3.78  2.12
 8  1876  0.94  4.81  8.79  3.06  3.03  2.66  3.65  2.28  5.28  1.42  3.31  2.54
 9  1877  2.62  1.24  5.56  2.73  0.95  2.8   5.73  2.77  1.33  8.14  5.62  0.68
10  1878  4.46  3.75  3.27  1.97  3.19  3.08  4.62  7.97  4.05  2.43  4.73  5.14
# … with 141 more rows, and 1 more variable: ANNUAL <dbl>

In the code above, we read the CSV data from a URL. You can read from a file too: just pass in a file path instead of a URL.