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

Importing Data with readr

Swipe um das Menü anzuzeigen

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?

Wählen Sie die richtige Antwort aus

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 2

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Abschnitt 1. Kapitel 2
some-alt