readr包:读取/输出文本数据

来源:互联网 发布:淘宝店刷流量 编辑:程序博客网 时间:2024/06/06 02:17

Hadley大神写的又一神器,可以方便的读入输出文本数据,且速度远远超过传统的函数。


读入数据

可以自动将文本数据读入为字符串格式,不需要设置factorAsString = FALSE

1. read_csv Read a delimited file (including csv & tsv) into a tibble
2. read_csv2 Read a delimited file (including csv & tsv) into a tibble

3. read_table Read whitespace-separated columns into a tibble
4. read_table2 Read whitespace-separated columns into a tibble
5. read_tsv Read a delimited file (including csv & tsv) into a tibble


6. read_delimRead a delimited file (including csv & tsv) into a tibble

根据间隔符读入数据

read_delim(file, delim, quote = "\"", col_names = TRUE, col_types = NULL)

仅写出常用的参数

delim:间隔符,单一字符,同read.table中的sep参数

quote:引用字符串的符号

col_names:logical第一行是否为列名称,或者使用字符串向量表示列名称

col_types:列类型,举例


一开始读入数据发现有些列类型解析错误,因此复制下来,更改错误的列类型,加入col_types参数,重新读取


7. read_fwf()

根据读取固定宽度的数据

read_fwf(file, col_positions, col_types = NULL)

col_positions说明:

个人使用两种参数表示形式

(1)fwf_positions(start, end = NULL, col_names = NULL)函数作为col_positions参数的值

其中start和end为数值向量,为起始的位置和结束的位置

例如:start = c(1, 8), end = c(6, NA)表示截取第1列到第6列的数据,和第8列到最后的数据,NA表示最后截取数据的长度不固定

或者

(2)fwf_cols(col1name = c(start, end), col2name = c(start,end)...)



输出数据

1. write_csv Write a data frame to a delimited file
2. write_excel_csv Write a data frame to a delimited file
3. write_tsv Write a data frame to a delimited file

4. write_delim Write a data frame to a delimited file

write_delim(x, path, delim = " ", na = "NA", append = FALSE, col_names = !append)

相对于write.table()函数,write_delim函数默认输出列名,factor强制转化为character,

除非必须(比如数据内部包含引号等),数据不会被引号包含

原创粉丝点击