打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
R语言 读取文件

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 <- readxl_example('datasets.xlsx') read_excel(xlsx_example)#> # A tibble: 150 x 5#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species#> <dbl> <dbl> <dbl> <dbl> <chr> #> 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 rowsxls_example <- readxl_example('datasets.xls') read_excel(xls_example)#> # A tibble: 150 x 5#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species#> <dbl> <dbl> <dbl> <dbl> <chr> #> 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 rowsList 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 #> <dbl> <chr> #> 1 179. horsebean#> 2 160. horsebean#> 3 136. horsebean#> # ... with 68 more rowsread_excel(xls_example, sheet = 4)#> # A tibble: 1,000 x 5#> lat long depth mag stations#> <dbl> <dbl> <dbl> <dbl> <dbl>#> 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 rowsThere 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#> <dbl> <dbl> <dbl> <dbl> <chr> #> 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 setosaread_excel(xlsx_example, range = 'C1:E4')#> # A tibble: 3 x 3#> Petal.Length Petal.Width Species#> <dbl> <dbl> <chr> #> 1 1.40 0.200 setosa #> 2 1.40 0.200 setosa #> 3 1.30 0.200 setosaread_excel(xlsx_example, range = cell_rows(1:4))#> # A tibble: 3 x 5#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species#> <dbl> <dbl> <dbl> <dbl> <chr> #> 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 setosaread_excel(xlsx_example, range = cell_cols('B:D'))#> # A tibble: 150 x 3#> Sepal.Width Petal.Length Petal.Width#> <dbl> <dbl> <dbl>#> 1 3.50 1.40 0.200#> 2 3.00 1.40 0.200#> 3 3.20 1.30 0.200#> # ... with 147 more rowsread_excel(xlsx_example, range = 'mtcars!B1:D5')#> # A tibble: 4 x 3#> cyl disp hp#> <dbl> <dbl> <dbl>#> 1 6. 160. 110.#> 2 6. 160. 110.#> 3 4. 108. 93.#> # ... with 1 more rowIf 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#> <dbl> <dbl> <dbl> <dbl> <chr> #> 1 5.10 3.50 1.40 0.200 <NA> #> 2 4.90 3.00 1.40 0.200 <NA> #> 3 4.70 3.20 1.30 0.200 <NA> #> # ... with 147 more rows

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
tidyverse|数据分析常规操作-分组汇总(sumamrise group_by)
tidyr总结篇-续
一个用R语言进行Kmeans聚类分析的例子
apply函数族入门
数据分析第一步:连续性数值变量统计描述常用SPSS及R操作过程
决策树与随机森林的R语言实现
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服