Importing Data with readr
Swipe to show menu
When you work with data in R, one of the first steps is often importing data from external files into your R environment. The readr package, which is part of the tidyverse, provides efficient way to read flat files such as CSVs, TSVs, and more. Compared to base R functions, readr functions are typically faster, produce tibbles (a modern reimagining of data frames), and offer a consistent and user-friendly interface. The most commonly used function for reading comma-separated value files is read_csv(), but readr also includes read_tsv(), read_delim(), and others for different file types.
12345678library(readr) options(crayon.enabled = FALSE) # Reading a CSV file into a tibble data <- read_csv("https://content-media-cdn.codefinity.com/courses/285f94af-e4a8-4436-a9d0-bb36a6dc39f4/iris.csv") # Viewing the first few rows print(head(data))
The read_csv() function is straightforward to use, but it also provides several arguments to handle various scenarios. Some of the most common arguments include:
file: the path to your CSV file;col_names: whether the first row contains column names (set toTRUEby default);col_types: specify the data types for columns, such as character, integer, or double;na: a character vector of strings to interpret as missing values (the default isc("", "NA"));skip: the number of lines to skip before reading data;n_max: the maximum number of rows to read.
Handling missing values is a frequent concern. By adjusting the na argument, you can tell read_csv() which strings in your data should be treated as NA in R. If your file uses "." or "NULL" to represent missing values, you can set na = c("", "NA", ".", "NULL") to ensure these are properly recognized.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Importing Data with readr
When you work with data in R, one of the first steps is often importing data from external files into your R environment. The readr package, which is part of the tidyverse, provides efficient way to read flat files such as CSVs, TSVs, and more. Compared to base R functions, readr functions are typically faster, produce tibbles (a modern reimagining of data frames), and offer a consistent and user-friendly interface. The most commonly used function for reading comma-separated value files is read_csv(), but readr also includes read_tsv(), read_delim(), and others for different file types.
12345678library(readr) options(crayon.enabled = FALSE) # Reading a CSV file into a tibble data <- read_csv("https://content-media-cdn.codefinity.com/courses/285f94af-e4a8-4436-a9d0-bb36a6dc39f4/iris.csv") # Viewing the first few rows print(head(data))
The read_csv() function is straightforward to use, but it also provides several arguments to handle various scenarios. Some of the most common arguments include:
file: the path to your CSV file;col_names: whether the first row contains column names (set toTRUEby default);col_types: specify the data types for columns, such as character, integer, or double;na: a character vector of strings to interpret as missing values (the default isc("", "NA"));skip: the number of lines to skip before reading data;n_max: the maximum number of rows to read.
Handling missing values is a frequent concern. By adjusting the na argument, you can tell read_csv() which strings in your data should be treated as NA in R. If your file uses "." or "NULL" to represent missing values, you can set na = c("", "NA", ".", "NULL") to ensure these are properly recognized.
Thanks for your feedback!