R语言|文件数据读入(readr包)

如题所述

与使用stringr包的函数处理字符串数据类似,我们在读取文件时也推荐使用readr包中的函数,而不是R基础包中的read.table()、read.csv()等。以下是原因:

1. read_*系列函数

这些函数用于将平面文件读入并转换为数据框。以下函数具有相同的语法,掌握其中一种即可掌握其他。

下面重点介绍read_csv()函数,因为csv文件是数据存储最常用的形式之一。

先来看一下read_csv()和read.csv()输出的区别

接下来,我们看一下read_csv()函数的参数:

read_csv(file, col_names = TRUE, col_types = NULL, col_select = NULL, id = NULL, locale = default_locale(), na = c("", "NA"), quoted_na = TRUE, quote = "\"", comment = "", trim_ws = TRUE, skip = 0, n_max = Inf, guess_max = min(1000, n_max), name_repair = "unique", num_threads = readr_threads(), progress = show_progress(), show_col_types = should_show_types(), skip_empty_rows = TRUE, lazy = TRUE)

2. write_*系列函数

readr包还提供了写入文件的函数。我们依然主要关注write_csv()函数。

write_csv(x, file, na = "NA", append = FALSE, col_names = !append, quote = c("needed", "all", "none"), escape = c("double", "backslash", "none"), eol = " ", num_threads = readr_threads(), progress = show_progress(), path = deprecated(), quote_escape = deprecated())
温馨提示:答案为网友推荐,仅供参考
相似回答
大家正在搜