read_csv("my/file.csv")Read a CSV file (.csv)
You want to read a CSV file into your R session, where you can manipulate its contents. The file has the extension .csv. A CSV file is a text file that contains a table whose values are separated by commas, i.e. a Comma Separated Values file.
Step 1 - Call readr::read_csv(). read_csv() is designed to read in .csv files with , as the field separator.
Step 2 - Give read_csv() the filepath to your file as a character string. R will read the filepath as if it begins at your working directory. For example:
Step 3 - Save the output to an object, so you can access it later.
csv_table <- read_csv("my/file.csv")Example
We want to read in the chickens dataset which is a CSV file. We have chickens saved on our computer at /Users/bcullen/Desktop/rstudio/academySassyRecipes/recipes/basics/data/chickens.csv.
We begin by loading the readr package which contains read_csv(). Next, we pass read_csv() the filepath for our CSV file in order to read in the dataset. The file is in the data folder. The output looks like this:
library(readr)
read_csv("data/chickens.csv")Rows: 5 Columns: 4
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (3): chicken, sex, motto
dbl (1): eggs_laid
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# A tibble: 5 × 4
chicken sex eggs_laid motto
<chr> <chr> <dbl> <chr>
1 Foghorn Leghorn rooster 0 That's a joke, ah say, that's a jok…
2 Chicken Little hen 3 The sky is falling!
3 Ginger hen 12 Listen. We'll either die free chick…
4 Camilla the Chicken hen 7 Bawk, buck, ba-gawk.
5 Ernie The Giant Chicken rooster 0 Put Captain Solo in the cargo hold.
Notice that read_csv() automatically chose intelligent data types for each of the columns.
Lastly, we assign the chickens dataset read in by read_csv() to an object, chickens, so we can access it later.
chickens <- read_csv("data/chickens.csv")Rows: 5 Columns: 4
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (3): chicken, sex, motto
dbl (1): eggs_laid
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
read_csv() comes with many arguments that you can use to customize which parts of the file will be read in and how. Here are a few of the most useful:
| Argument | Description |
|---|---|
col_names |
Should the first row be read in as column names? Defaults to TRUE. Can also be a character vector of column names. |
col_types |
Explicitly set the data type for each column. |
skip |
Number of rows to skip before reading any data. |
guess_max |
Maximum number of lines to read for guessing column types. |
Read the help page at ?read_csv to learn more.
read_csv() and SAS
read_csv() is the equivalent of SAS’s IMPORT procedure with the DATAFILE option and DBMS = CSV. In SAS, you must specify the complete path, filename, and extension for DATAFILE, whereas when using read_csv() the path can be specified based on the working directory.
In SAS:
PROC IMPORT DATAFILE = 'filename' OUT = csv_table
DBMS = CSV REPLACE;
RUN;In R:
csv_table <- read_csv("my/file.csv")