1. 程式人生 > >學習筆記(二)tidyverse之readxl包------表格資料讀取

學習筆記(二)tidyverse之readxl包------表格資料讀取

1、readxl概況

readxl包是tidyverse中的一員,是匯入Excel表格資料的一個R包,由Hadley Wickham開發的。與其他已經存在的包(例如:gdata包、xlsx包…)最大的區別是不依賴其他外部程式,能夠在所有作業系統中都方便使用; 主要功能是匯入xls/xlsx 檔案; 官方連結:

2、installation & library

方法一:從CRAN中安裝在整個tidyverse包

#install.packages("tidyverse")
library(tidyverse)

## Warning: package 'tidyverse' was built under R version 3.4.4

## -- Attaching packages ---------------------------------- tidyverse 1.2.1 --

## √ ggplot2 2.2.1     √ purrr   0.2.4 ## √ tibble  1.4.2     √ dplyr   0.7.4 ## √ tidyr   0.8.0     √ stringr 1.2.0 ## √ readr   1.1.1     √ forcats 0.3.0

## Warning: package 'ggplot2' was built under R version 3.4.1

## Warning: package 'tibble' was built under R version 3.4.4

## Warning: package 'tidyr' was built under R version 3.4.4

## Warning: package 'readr' was built under R version 3.4.4

## Warning: package 'purrr' was built under R version 3.4.4

## Warning: package 'dplyr' was built under R version 3.4.3

## Warning: package 'stringr' was built under R version 3.4.1

## Warning: package 'forcats' was built under R version 3.4.4

## -- Conflicts ------------------------------------- tidyverse_conflicts() -- ## x dplyr::filter() masks stats::filter() ## x dplyr::lag()    masks stats::lag()

方法二:從Cran中直接安裝readxl包

#install.packages("readxl")
library(readxl)

## Warning: package 'readxl' was built under R version 3.4.4

3、Usage

readxl_example()

readxl包中含有一些例子,我們可以使用readxl_example()函式直接將它們展示出來,或者使用readxl_example("filename"")得到該檔案的路徑

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("datasets.xls")

## [1] "D:/R-3.4.0/library/readxl/extdata/datasets.xls"

readxl_example("datasets.xlsx")

## [1] "D:/R-3.4.0/library/readxl/extdata/datasets.xlsx"

注:接下來使用datasets.xls及datasets.xlsx來分享readxl包中其他函式的使用方法

read_excel()

匯入xls/xlsx函式

xls_eg <- read_excel("D:/R-3.4.0/library/readxl/extdata/datasets.xls")
xls_eg

## # A tibble: 150 x 5 ##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species ##           <dbl>       <dbl>        <dbl>       <dbl> <chr>  ##  1          5.1         3.5          1.4         0.2 setosa ##  2          4.9         3            1.4         0.2 setosa ##  3          4.7         3.2          1.3         0.2 setosa ##  4          4.6         3.1          1.5         0.2 setosa ##  5          5           3.6          1.4         0.2 setosa ##  6          5.4         3.9          1.7         0.4 setosa ##  7          4.6         3.4          1.4         0.3 setosa ##  8          5           3.4          1.5         0.2 setosa ##  9          4.4         2.9          1.4         0.2 setosa ## 10          4.9         3.1          1.5         0.1 setosa ## # ... with 140 more rows

xlsx_eg <- read_excel("D:/R-3.4.0/library/readxl/extdata/datasets.xlsx")
xlsx_eg

## # A tibble: 150 x 5 ##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species ##           <dbl>       <dbl>        <dbl>       <dbl> <chr>  ##  1          5.1         3.5          1.4         0.2 setosa ##  2          4.9         3            1.4         0.2 setosa ##  3          4.7         3.2          1.3         0.2 setosa ##  4          4.6         3.1          1.5         0.2 setosa ##  5          5           3.6          1.4         0.2 setosa ##  6          5.4         3.9          1.7         0.4 setosa ##  7          4.6         3.4          1.4         0.3 setosa ##  8          5           3.4          1.5         0.2 setosa ##  9          4.4         2.9          1.4         0.2 setosa ## 10          4.9         3.1          1.5         0.1 setosa ## # ... with 140 more rows

其他引數: sheet

sheet 匯入某個特定的工作表,預設第一個工作表
range 匯入表格中特定區域的資料, 預設全區域
col_names 列名,預設為TRUE(第一行為列名),設定為F, 則列名為X1、X2…,也可通過一個向量設定 特定列名
col_type 列的資料型別
n_max 設定讀取最大的行數
na 預設表示資料中的缺失值,也可以將特定值設定為 缺失值

excel_sheets()

列出Excel檔案中所有工作表名稱

excel_sheets("D:/R-3.4.0/library/readxl/extdata/datasets.xls")

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

4、Case

excel_sheets("D:/R-3.4.0/library/readxl/extdata/datasets.xls")

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

xls_iris <- read_excel("D:/R-3.4.0/library/readxl/extdata/datasets.xls", sheet =1)
xls_iris

## # A tibble: 150 x 5 ##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species ##           <dbl>       <dbl>        <dbl>       <dbl> <chr>  ##  1          5.1         3.5          1.4         0.2 setosa ##  2          4.9         3            1.4         0.2 setosa ##  3          4.7         3.2          1.3         0.2 setosa ##  4          4.6         3.1          1.5         0.2 setosa ##  5          5           3.6          1.4         0.2 setosa ##  6          5.4         3.9          1.7         0.4 setosa ##  7          4.6         3.4          1.4         0.3 setosa ##  8          5           3.4          1.5         0.2 setosa ##  9          4.4         2.9          1.4         0.2 setosa ## 10          4.9         3.1          1.5         0.1 setosa ## # ... with 140 more rows

xls_iris1 <- read_excel("D:/R-3.4.0/library/readxl/extdata/datasets.xls", range = "A1:E21")
xls_iris1

## # A tibble: 20 x 5 ##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species ##           <dbl>       <dbl>        <dbl>       <dbl> <chr>  ##  1          5.1         3.5          1.4         0.2 setosa ##  2          4.9         3            1.4         0.2 setosa ##  3          4.7         3.2          1.3         0.2 setosa ##  4          4.6         3.1          1.5         0.2 setosa ##  5          5           3.6          1.4         0.2 setosa ##  6          5.4         3.9          1.7         0.4 setosa ##  7          4.6         3.4          1.4         0.3 setosa ##  8          5           3.4          1.5         0.2 setosa ##  9          4.4         2.9          1.4         0.2 setosa ## 10          4.9         3.1          1.5         0.1 setosa ## 11          5.4         3.7          1.5         0.2 setosa ## 12          4.8         3.4          1.6         0.2 setosa ## 13          4.8         3            1.4         0.1 setosa ## 14          4.3         3            1.1         0.1 setosa ## 15          5.8         4            1.2         0.2 setosa ## 16          5.7         4.4          1.5         0.4 setosa ## 17          5.4         3.9          1.3         0.4 setosa ## 18          5.1         3.5          1.4         0.3 setosa ## 19          5.7         3.8          1.7         0.3 setosa ## 20          5.1         3.8          1.5         0.3 setosa

xls_iris2 <- read_excel("D:/R-3.4.0/library/readxl/extdata/datasets.xls", n_max = 20)
xls_iris2

## # A tibble: 20 x 5 ##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species ##           <dbl>       <dbl>        <dbl>       <dbl> <chr>  ##  1          5.1         3.5          1.4         0.2 setosa ##  2          4.9         3            1.4         0.2 setosa ##  3          4.7         3.2          1.3         0.2 setosa ##  4          4.6         3.1          1.5         0.2 setosa ##  5          5           3.6          1.4         0.2 setosa ##  6          5.4         3.9          1.7         0.4 setosa ##  7          4.6         3.4          1.4         0.3 setosa ##  8          5           3.4          1.5         0.2 setosa ##  9          4.4         2.9          1.4         0.2 setosa ## 10          4.9         3.1          1.5         0.1 setosa ## 11          5.4         3.7          1.5         0.2 setosa ## 12          4.8         3.4          1.6         0.2 setosa ## 13          4.8         3            1.4         0.1 setosa ## 14          4.3         3            1.1         0.1 setosa ## 15          5.8         4            1.2         0.2 setosa ## 16          5.7         4.4          1.5         0.4 setosa ## 17          5.4         3.9          1.3         0.4 setosa ## 18          5.1         3.5          1.4         0.3 setosa ## 19          5.7         3.8          1.7         0.3 setosa ## 20          5.1         3.8          1.5         0.3 setosa

xls_iris3 <- read_excel("D:/R-3.4.0/library/readxl/extdata/datasets.xls", range = cell_cols("B:D"))
xls_iris3

## # A tibble: 150 x 3 ##    Sepal.Width Petal.Length Petal.Width ##          <dbl>        <dbl>       <dbl> ##  1         3.5          1.4         0.2 ##  2         3            1.4         0.2 ##  3         3.2          1.3         0.2 ##  4         3.1          1.5         0.2 ##  5         3.6          1.4         0.2 ##  6         3.9          1.7         0.4 ##  7         3.4          1.4         0.3 ##  8         3.4          1.5         0.2 ##  9         2.9          1.4         0.2 ## 10         3.1          1.5         0.1 ## # ... with 140 more rows

xls_iris4 <- read_excel("D:/R-3.4.0/library/readxl/extdata/datasets.xls", col_names = FALSE)
xls_iris4

## # A tibble: 151 x 5 ##    X__1               X__2               X__3               X__4     X__5 ##    <chr>              <chr>              <chr>              <chr>    <chr> ##  1 Sepal.Length       Sepal.Width        Petal.Length       Petal.W~ Spec~ ##  2 5.0999999999999996 3.5                1.3999999999999999 0.20000~ seto~ ##  3 4.9000000000000004 3                  1.3999999999999999 0.20000~ seto~ ##  4 4.7000000000000002 3.2000000000000002 1.3                0.20000~ seto~ ##  5 4.5999999999999996 3.1000000000000001 1.5                0.20000~ seto~ ##  6 5                  3.6000000000000001 1.3999999999999999 0.20000~ seto~ ##  7 5.4000000000000004 3.8999999999999999 1.7                0.40000~ seto~ ##  8 4.5999999999999996 3.3999999999999999 1.3999999999999999 0.29999~ seto~ ##  9 5                  3.3999999999999999 1.5                0.20000~ seto~ ## 10 4.4000000000000004 2.8999999999999999 1.3999999999999999 0.20000~ seto~ ## # ... with 141 more rows

xls_iris5 <- read_excel("D:/R-3.4.0/library/readxl/extdata/datasets.xls", col_names = c("x1","x2","x3","x4","y"))
xls_iris5

## # A tibble: 151 x 5 ##    x1                 x2                 x3                 x4       y    ##    <chr>              <chr>              <chr>              <chr>    <chr> ##  1 Sepal.Length       Sepal.Width        Petal.Length       Petal.W~ Spec~ ##  2 5.0999999999999996 3.5                1.3999999999999999 0.20000~ seto~ ##  3 4.9000000000000004 3                  1.3999999999999999 0.20000~ seto~ ##  4 4.7000000000000002 3.2000000000000002 1.3                0.20000~ seto~ ##  5 4.5999999999999996 3.1000000000000001 1.5                0.20000~ seto~ ##  6 5                  3.6000000000000001 1.3999999999999999 0.20000~ seto~ ##  7 5.4000000000000004 3.8999999999999999 1.7                0.40000~ seto~ ##  8 4.5999999999999996 3.3999999999999999 1.3999999999999999 0.29999~ seto~ ##  9 5                  3.3999999999999999 1.5                0.20000~ seto~ ## 10 4.4000000000000004 2.8999999999999999 1.3999999999999999 0.20000~ seto~ ## # ... with 141 more rows

xls_iris6 <- read_excel("D:/R-3.4.0/library/readxl/extdata/datasets.xls", col_names = FALSE, col_types = "numeric")

xls_iris6

## Warning in read_fun(path = path, sheet_i = sheet, limits = limits, shim = ## shim, : Expecting numeric in A1 / R1C1: got 'Sepal.Length'

## Warning in read_fun(path = path, sheet_i = sheet, limits = limits, shim = ## shim, : Expecting numeric in B1 / R1C2: got 'Sepal.Width'

## Warning in read_fun(path = path, sheet_i = sheet, limits = limits, shim = ## shim, : Expecting numeric in C1 / R1C3: got 'Petal.Length'

## Warning in read_fun(path = path, sheet_i = sheet, limits = limits, shim = ## shim, : Expecting numeric in D1 / R1C4: got 'Petal.Width'

## Warning in read_fun(path = path, sheet_i = sheet, limits = limits, shim = ## shim, : Expecting numeric in E1 / R1C5: got 'Species'

## # A tibble: 151 x 5 ##     X__1  X__2  X__3  X__4  X__5 ##    <dbl> <dbl> <dbl> <dbl> <dbl> ##  1  NA    NA    NA    NA      NA ##  2   5.1   3.5   1.4   0.2    NA ##  3   4.9   3     1.4   0.2    NA ##  4   4.7   3.2   1.3   0.2    NA ##  5   4.6   3.1   1.5   0.2    NA ##  6   5     3.6   1.4   0.2    NA ##  7   5.4   3.9   1.7   0.4    NA ##  8   4.6   3.4   1.4   0.3    NA ##  9   5     3.4   1.5   0.2    NA ## 10   4.4   2.9   1.4   0.2    NA ## # ... with 141 more rows

xls_iris7 <- read_excel("D:/R-3.4.0/library/readxl/extdata/datasets.xls", col_names = c("x1","x2","x3","x4","y"), col_types = c("numeric","numeric","numeric","numeric","text"))

xls_iris7

## Warning in read_fun(path = path, sheet_i = sheet, limits = limits, shim = ## shim, : Expecting numeric in A1 / R1C1: got 'Sepal.Length'

## Warning in read_fun(path = path, sheet_i = sheet, limits = limits, shim = ## shim, : Expecting numeric in B1 / R1C2: got 'Sepal.Width'

## Warning in read_fun(path = path, sheet_i = sheet, limits = limits, shim = ## shim, : Expecting numeric in C1 / R1C3: got 'Petal.Length'

## Warning in read_fun(path = path, sheet_i = sheet, limits = limits, shim = ## shim, : Expecting numeric in D1 / R1C4: got 'Petal.Width'

## # A tibble: 151 x 5 ##       x1    x2    x3    x4 y      ##    <dbl> <dbl> <dbl> <dbl> <chr>  ##  1  NA    NA    NA    NA   Species ##  2   5.1   3.5   1.4   0.2 setosa ##  3   4.9   3     1.4   0.2 setosa ##  4   4.7   3.2   1.3   0.2 setosa ##  5   4.6   3.1   1.5   0.2 setosa ##  6   5     3.6   1.4   0.2 setosa ##  7   5.4   3.9   1.7   0.4 setosa ##  8   4.6   3.4   1.4   0.3 setosa ##  9   5     3.4   1.5   0.2 setosa ## 10   4.4   2.9   1.4   0.2 setosa ## # ... with 141 more rows

xls_iris8 <- read_excel("D:/R-3.4.0/library/readxl/extdata/datasets.xls",na = "setosa")
xls_iris8

## # A tibble: 150 x 5 ##    Sepal.Length Sepal.Width Petal.Length Petal.Width Species ##           <dbl>       <dbl>        <dbl>       <dbl> <chr>  ##  1          5.1         3.5          1.4         0.2 <NA>   ##  2          4.9         3            1.4         0.2 <NA>   ##  3          4.7         3.2          1.3         0.2 <NA>   ##  4          4.6         3.1          1.5         0.2 <NA>   ##  5          5           3.6          1.4         0.2 <NA>   ##  6          5.4         3.9          1.7         0.4 <NA>   ##  7          4.6         3.4          1.4         0.3 <NA>   ##  8          5           3.4          1.5         0.2 <NA>   ##  9          4.4         2.9          1.4         0.2 <NA>   ## 10          4.9         3.1          1.5         0.1 <NA>   ## # ... with 140 more rows

xls_mtcars <- read_excel("D:/R-3.4.0/library/readxl/extdata/datasets.xls", sheet = 2)
xls_mtcars

## # A tibble: 32 x 11 ##      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb ##    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> ##  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4 ##  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4 ##  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1 ##  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1 ##  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2 ##  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1 ##  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4 ##  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2 ##  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2 ## 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4 ## # ... with 22 more rows

xls_mtcars1 <- read_excel("D:/R-3.4.0/library/readxl/extdata/datasets.xls", sheet  = "mtcars")
xls_mtcars1

## # A tibble: 32 x 11 ##      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb ##    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> ##  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4 ##  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4 ##  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1 ##  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1 ##  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2 ##  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1 ##  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4 ##  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2 ##  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2 ## 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4 ## # ... with 22 more rows

xls_mtcars2 <- read_excel("D:/R-3.4.0/library/readxl/extdata/datasets.xls", range = "mtcars!a1:f10")
xls_mtcars2

## # A tibble: 9 x 6 ##     mpg   cyl  disp    hp  drat    wt ##   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> ## 1  21       6  160    110  3.9   2.62 ## 2  21       6  160    110  3.9   2.88 ## 3  22.8     4  108     93  3.85  2.32 ## 4  21.4     6  258    110  3.08  3.22 ## 5  18.7     8  360    175  3.15  3.44 ## 6  18.1     6  225    105  2.76  3.46 ## 7  14.3     8  360    245  3.21  3.57 ## 8  24.4     4  147.    62  3.69  3.19 ## 9  22.8     4  141.    95  3.92  3.15

xls_mtcars3 <- read_excel("D:/R-3.4.0/library/readxl/extdata/datasets.xls", sheet =2, range = cell_rows(1:11))
xls_mtcars3

## # A tibble: 10 x 11 ##      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb ##    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> ##  1  21       6  160    110  3.9   2.62  16.5     0     1     4     4 ##  2  21       6  160    110  3.9   2.88  17.0     0     1     4     4 ##  3  22.8     4  108     93  3.85  2.32  18.6     1     1     4     1 ##  4  21.4     6  258    110  3.08  3.22  19.4     1     0     3     1 ##  5  18.7     8  360    175  3.15  3.44  17.0     0     0     3     2 ##  6  18.1     6  225    105  2.76  3.46  20.2     1     0     3     1 ##  7  14.3     8  360    245  3.21  3.57  15.8     0     0     3     4 ##  8  24.4     4  147.    62  3.69  3.19  20       1     0     4     2 ##  9  22.8     4  141.    95  3.92  3.15  22.9     1     0     4     2 ## 10  19.2     6  168.   123  3.92  3.44  18.3     1     0     4     4