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

bookImporting 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 a modern and 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) # Read a CSV file into a tibble data <- read_csv("https://staging-content-media-cdn.codefinity.com/courses/285f94af-e4a8-4436-a9d0-bb36a6dc39f4/iris.csv") # View the first few rows print(head(data))
copy

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. For example, 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?

正しい答えを選んでください

すべて明確でしたか?

どのように改善できますか?

フィードバックありがとうございます!

セクション 1.  2

AIに質問する

expand

AIに質問する

ChatGPT

何でも質問するか、提案された質問の1つを試してチャットを始めてください

セクション 1.  2
some-alt