r语言读取matlab文件是否存在,R语言 读取文件

1. R读取txt文件

使用R读取txt文件直接使用read.table()方法进行读取即可,不需要加载额外的包。

read.table("/home/slave/test.txt",header=T,na.strings = c("NA"))

注意,此处的na.strings = c("NA") 的意思是文件中的缺失数据都是用NA进行表示;在读取文本文件时,默认的分割符号为空格。具体的参数设置可参照如下:

read.table(file, header = FALSE, sep = "", quote = "\"'",

dec = ".", numerals = c("allow.loss", "warn.loss", "no.loss"),

row.names, col.names, as.is = !stringsAsFactors,

na.strings = "NA", colClasses = NA, nrows = -1,

skip = 0, check.names = TRUE, fill = !blank.lines.skip,

strip.white = FALSE, blank.lines.skip = TRUE,

comment.char = "#",

allowEscapes = FALSE, flush = FALSE,

stringsAsFactors = default.stringsAsFactors(),

fileEncoding = "", encoding = "unknown", text, skipNul = FALSE)

19988751be7b

2. R读取csv文件

使用R读取csv文件和读取txt文件很类似,使用的是read.csv()方法,两者参数的使用大部分是一样的。

read.csv("/home/slave/test.csv", header=T, na.strings=c("NA"))

read.csv(file, header = TRUE, sep = ",", quote = "\"",

dec = ".", fill = TRUE, comment.char = "", ...)

3. R读取xls和xlsx文件

读取xls和xlsx有很多方法,但是这里面的很多方法也不是特别好用,例如RODBC包中的读取xls方法就不太好用,有时还会出现各种各样的问题。在进行了一番入坑探索之后,找到了两个相对好用的读取xls文件的包,下面我将分别进行说明。

gdata

install.packages("gdata")

library(gdata)

read.xls("/home/slave/test.xls",sheet=1,na.strings=c("NA","#DIV/0!"))

其中sheet=1 参数的意思是读取第一个sheet中的内容;na.strings=c("NA","#DIV/0!") 将"NA" 和 "#DIV/0!" 都作为缺失数据表示,read.xls()方法的具体参数设置可参考如下:

read.xls(xls, sheet=1, verbose=FALSE, pattern, na.strings=c("NA","#DIV/0!"),

..., method=c("csv","tsv","tab"), perl="perl")

![](http://upload-images.jianshu.io/upload_images/9218360-3f24ba87157d2f8e?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

xls2csv(xls, sheet=1, verbose=FALSE, blank.lines.skip=TRUE, ..., perl="perl")

xls2tab(xls, sheet=1, verbose=FALSE, blank.lines.skip=TRUE, ..., perl="perl")

xls2tsv(xls, sheet=1, verbose=FALSE, blank.lines.skip=TRUE, ..., perl="perl")

xls2sep(xls, sheet=1, verbose=FALSE, blank.lines.skip=TRUE, ...,

method=c("csv","tsv","tab"), perl="perl")

gdata包有着很多的功能,但是它对其他的包的依赖很多,可能会出现各种不可预知的问题,下面介绍一个较少依赖的包。

readxl

install.packages("readxl")

library(readxl)

read_excel("/home/slave/test.xls",sheet=1,na="NA")

read_excel(path, sheet = 1, col_names = TRUE, col_types = NULL, na = "", skip = 0)

19988751be7b

readxl软件包可以很容易地将数据从Excel和R中取出。与许多现有软件包(例如gdata,xlsx,xlsReadWrite)相比,readxl没有外部依赖关系,因此它很容易在所有操作系统上安装和使用。 它旨在与表格数据一起工作。readxl支持旧版.xls格式和现代基于xml的.xlsx格式。

用法

library(readxl)

readxl includes several example files, which we use throughout the documentation. Use the helper readxl_example() with no arguments to list them or call it with an example filename to get the path.

readxl_example()

#> [1] "clippy.xls" "clippy.xlsx" "datasets.xls" "datasets.xlsx"

#> [5] "deaths.xls" "deaths.xlsx" "geometry.xls" "geometry.xlsx"

#> [9] "type-me.xls" "type-me.xlsx"

readxl_example("clippy.xls")

#> [1] "/Users/jenny/resources/R/library/readxl/extdata/clippy.xls"

read_excel() reads both xls and xlsx files and detects the format from the extension.

xlsx_example

read_excel(xlsx_example)

#> # A tibble: 150 x 5

#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species

#>

#> 1 5.10 3.50 1.40 0.200 setosa

#> 2 4.90 3.00 1.40 0.200 setosa

#> 3 4.70 3.20 1.30 0.200 setosa

#> # ... with 147 more rows

xls_example

read_excel(xls_example)

#> # A tibble: 150 x 5

#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species

#>

#> 1 5.10 3.50 1.40 0.200 setosa

#> 2 4.90 3.00 1.40 0.200 setosa

#> 3 4.70 3.20 1.30 0.200 setosa

#> # ... with 147 more rows

List the sheet names with excel_sheets().

excel_sheets(xlsx_example)

#> [1] "iris" "mtcars" "chickwts" "quakes"

Specify a worksheet by name or number.

read_excel(xlsx_example, sheet = "chickwts")

#> # A tibble: 71 x 2

#> weight feed

#>

#> 1 179. horsebean

#> 2 160. horsebean

#> 3 136. horsebean

#> # ... with 68 more rows

read_excel(xls_example, sheet = 4)

#> # A tibble: 1,000 x 5

#> lat long depth mag stations

#>

#> 1 -20.4 182. 562. 4.80 41.

#> 2 -20.6 181. 650. 4.20 15.

#> 3 -26.0 184. 42. 5.40 43.

#> # ... with 997 more rows

There are various ways to control which cells are read. You can even specify the sheet here, if providing an Excel-style cell range.

read_excel(xlsx_example, n_max = 3)

#> # A tibble: 3 x 5

#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species

#>

#> 1 5.10 3.50 1.40 0.200 setosa

#> 2 4.90 3.00 1.40 0.200 setosa

#> 3 4.70 3.20 1.30 0.200 setosa

read_excel(xlsx_example, range = "C1:E4")

#> # A tibble: 3 x 3

#> Petal.Length Petal.Width Species

#>

#> 1 1.40 0.200 setosa

#> 2 1.40 0.200 setosa

#> 3 1.30 0.200 setosa

read_excel(xlsx_example, range = cell_rows(1:4))

#> # A tibble: 3 x 5

#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species

#>

#> 1 5.10 3.50 1.40 0.200 setosa

#> 2 4.90 3.00 1.40 0.200 setosa

#> 3 4.70 3.20 1.30 0.200 setosa

read_excel(xlsx_example, range = cell_cols("B:D"))

#> # A tibble: 150 x 3

#> Sepal.Width Petal.Length Petal.Width

#>

#> 1 3.50 1.40 0.200

#> 2 3.00 1.40 0.200

#> 3 3.20 1.30 0.200

#> # ... with 147 more rows

read_excel(xlsx_example, range = "mtcars!B1:D5")

#> # A tibble: 4 x 3

#> cyl disp hp

#>

#> 1 6. 160. 110.

#> 2 6. 160. 110.

#> 3 4. 108. 93.

#> # ... with 1 more row

If NAs are represented by something other than blank cells, set the na argument.

read_excel(xlsx_example, na = "setosa")

#> # A tibble: 150 x 5

#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species

#>

#> 1 5.10 3.50 1.40 0.200

#> 2 4.90 3.00 1.40 0.200

#> 3 4.70 3.20 1.30 0.200

#> # ... with 147 more rows

特征

没有外部依赖,例如Java或Perl。

将非ASCII字符重新编码为UTF-8。

将日期时间加载到POSIXct列中。 Windows(1900)和Mac(1904)日期规格都正确处理。

发现最小数据矩形并默认返回。 用户可以使用范围,跳过和n_max进行更多的控制。

默认情况下,列名称和类型由工作表中的数据确定。 用户也可以通过col_names和col_types提供。

返回一个tibble,即带有附加tbl_df类的数据框。 除此之外,这提供更好的打印。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值