tidyverse學習與總結
引言
tidyverse 對於R語言的數據科學應用,其重要性不用多言。R語言程序包數量居多,但是這個tidyverse 套裝中的程序包值得我去認真學習和總結,以期獲得一個鳥瞰視野。
首先,查看其包含的所有包,目前一共26個(截止2020-4-7):
library(tidyverse)
# List all packages in the tidyverse
tidyverse_packages(include_self = TRUE)
[1] "broom" "cli" "crayon" "dplyr" "dbplyr" "forcats"
[7] "ggplot2" "haven" "hms" "httr" "jsonlite" "lubridate"
[13] "magrittr" "modelr" "purrr" "readr" "readxl\n(>=" "reprex"
[19] "rlang" "rstudioapi" "rvest" "stringr" "tibble" "tidyr"
[25] "xml2" "tidyverse"
# 注意readxl \n(>= 是顯示問題,其實就是readxl package
然后按照個人興趣,逐個學習其中的每個包的主要函數與用法,與語法邏輯。
對於對一個子package,我強烈推薦通過browseVignettes(package = "pkg_name")
,pkg_name是子package 的包名稱,來進行鳥瞰式地學習和掌握該package。
broom
browseVignettes(package = "broom")
Vignettes in package broom
- Adding tidiers to broom - HTML source R code
- Available methods - HTML source R code
- broom and dplyr - HTML source R code
- Glossary of ouput columns and tidier arguments - HTML source R code
- Introduction to broom - HTML source R code
- kmeans with dplyr and broom - HTML source R code
- Tidy bootstrapping - HTML source R code
cli
crayon
dplyr
browseVignettes(package = "dplyr")
打開一個html頁面,
Vignettes in package dplyr
- dplyr compatibility - HTML source R code
- Introduction to dplyr - HTML source R code
- Programming with dplyr - HTML source R code
- Two-table verbs - HTML source R code
- Window functions - HTML source R code
看到包含5個vignette,每個都包含了html, source,和R code。這三個文件的邏輯是,source為R markdown的代碼,html 是source rmarkdown文檔導出的html文件,R code 就是只包含html 中的R代碼部分(Just the code)。所以為了學習目的,應該看html,既有詳細的介紹,又有代碼示例。
- 首先學習,Introduction to dplyr的html。是dplyr 語法基礎和函數基礎的介紹,是必須掌握的內容。
- 再學習了,Two-table verbs。介紹了Mutating joins, Filtering joins, Set operations,是在dplyr中處理兩個數據框之間根據觀測值、根據變量名、將數據行視為一個集合的三種類似於merge用法的函數。
- 然后再學習Window functions。窗口函數就是聚合函數的變體,窗口函數產生和變量個數一樣數目的結果,聚合函數如mean()將n個變量產生出一個結果。
- 再學習了Programming with dplyr 是一個dplyr函數中,引用的列名中包含變量時的處理方法。
- dplyr compatibility,主要是介紹不同的dplyr包版本之間的兼容性,目前一般的使用中一般用到的都是版本之間一直保持的經典函數用法,所以這個文檔可以先不做學習。
dbplyr
forcats
ggplot2
haven
hms
httr
jsonlite
lubridate
magrittr
modelr
browseVignettes(package = "modelr")
提示沒有vignette。
help("modelr")
modelr: Modelling Functions that Work with the Pipe。modelr,幫助將建模過程無縫集成數據處理和可視化的管道中。
purrr
其他語言的函數式編程
browseVignettes(package = "purrr")
Vignettes in package purrr
本package的html內容介紹比較簡略,如下:
purrr從許多相關的工具中獲得靈感:
- List operations defined in the Haskell prelude
- Scala’s list methods.
- Functional programming libraries for javascript: underscore.js, lodash and lazy.js.
- rlist, another R package to support working with lists. Similar goals but somewhat different philosophy.
然而,purrr的目標並不是試圖在R中模擬一種更純粹的函數式編程語言;我們不想在R中實現Haskell的第二級R版本。我們的目標是給你類似於FP語言的表達能力,同時允許你寫代碼看起來和工作起來都像R:
- 我們使用管道
%>%
, 來編寫可以從左向右讀取的代碼,而不是使用 point free(默示)樣式。. - 我們用
…
來傳遞額外的參數。 - 匿名函數在R中是冗長的,因此我們提供了兩個簡便方法。對於一元函數,
~ .x + 1
is equivalent tofunction(.x) .x + 1
。對於函數鏈,. %>% f() %>% g()
is equivalent tofunction(.) . %>% f() %>% g()
(此快捷方式由magrittr提供)。 - R是弱類型的,所以我們需要' map '變量來描述輸出類型(如
map_int()
,map_dbl()
, 等),因為我們不知道'.f
的返回類型。 - R函數支持name arguments,而不是為一些細微的變化提供不同的函數。' detect() '和' detectLast() ')我們使用一個命名參數' .right '。類型穩定的函數很容易推理,因此附加的參數永遠不會改變輸出的類型。R has named arguments, so instead of providing different functions for minor variations (e.g.
detect()
anddetectLast()
) we use a named argument,.right
. Type-stable functions are easy to reason about so additional arguments will never change the type of the output.
readr
Vignettes in package readr
readxl
reprex
rlang
rstudioapi
rvest
stringr
browseVignettes(package = "stringr")
Vignettes in package stringr
首先,學習Introduction to stringr
在 stringr主要有四個家族的函數:
- 字符操作函數
- 空格操作工具
- 區域敏感操作,其操作因區域而異。
- 模式匹配功能。它們識別模式描述的四個引擎。最常見的是正則表達式,但還有其他三種工具。