Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Importing Data with readr | Section
Practical Data Preparation in R with Tidyverse

Importing Data with readr

Desliza para mostrar el menú

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.

12345678
library(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 to TRUE by 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 is c("", "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.

question mark

Which of the following is a key difference between read_csv() from the readr package and base R's read.csv() function?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 2

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Sección 1. Capítulo 2
some-alt