0
点赞
收藏
分享

微信扫一扫

R语言观察日志(part5)--利用readr和readxl包读写数据

学习笔记,仅供参考


利用readr和readxl包读写数据



读取数据



  • 相关函数


函数包readr和readxl提供了一系列的数据读入功能,主要函数如下:

#readr包

read_delim(file, delim, quote = "\"", escape_backslash = FALSE,
escape_double = TRUE, col_names = TRUE, col_types = NULL,
locale = default_locale(), na = c("", "NA"), quoted_na = TRUE,
comment = "", trim_ws = FALSE, skip = 0, n_max = Inf,
guess_max = min(1000, n_max), progress = show_progress(),
skip_empty_rows = TRUE)

read_csv(file, col_names = TRUE, col_types = 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),
progress = show_progress(), skip_empty_rows = TRUE)

#readxl包
read_excel(path, sheet = NULL, range = NULL, col_names = TRUE,
col_types = NULL, na = "", trim_ws = TRUE, skip = 0,
n_max = Inf, guess_max = min(1000, n_max),
progress = readxl_progress(), .name_repair = "unique")

read_xls(path, sheet = NULL, range = NULL, col_names = TRUE,
col_types = NULL, na = "", trim_ws = TRUE, skip = 0,
n_max = Inf, guess_max = min(1000, n_max),
progress = readxl_progress(), .name_repair = "unique")



  • 参数

R语言观察日志(part5)--利用readr和readxl包读写数据_读取数据

R语言观察日志(part5)--利用readr和readxl包读写数据_读取数据_02



  • 举个例子

输入:

library(readr)
library(readxl)

cp <-read_delim("comp.csv", ",")
cp.csv <- read_csv("comp.csv")
cp.xl <- read_excel("comp.xlsx")
#summary(cp.csv)
#summary(cp.xl)

system.time(read_csv("data.csv"))
system.time(read.csv("data.csv"))

输出:

> system.time(read_csv("data.csv"))
用户 系统 流逝
0.88 0.00 0.89
Warning message:
Duplicated column names deduplicated: 'DATE_R' => 'DATE_R_1' [48]
> system.time(read.csv("data.csv"))
用户 系统 流逝
3.77 0.05 3.86

通过与R中的read.csv()函数进行比对,我们发现,利用函数包readr和readxl中的函数进行数据读入的速度有很大提升。



写入数据



函数包readr提供了数据读取功能的同时,还提供了数据写入功能,即将data.frame对象重新写为csv, xlsx,等格式的文件。



  • 相关函数
write_delim(x, path, delim = " ", na = "NA", append = FALSE,
col_names = !append, quote_escape = "double")

write_csv(x, path, na = "NA", append = FALSE, col_names = !append,
quote_escape = "double")

write_excel_csv(x, path, na = "NA", append = FALSE,
col_names = !append, delim = ",", quote_escape = "double")



  • 参数

R语言观察日志(part5)--利用readr和readxl包读写数据_数据_03



  • 举个例子

输入:

df <- data.frame(x = c(1,2,3,4,5), y = c(6,7,NA,9,0))
write_delim(df, "df1.csv", delim = ",")
write_csv(df, "df2.csv", na = "-")

df1.csv:

R语言观察日志(part5)--利用readr和readxl包读写数据_数据_04

df2.csv:

R语言观察日志(part5)--利用readr和readxl包读写数据_数据_05

举报

相关推荐

0 条评论