1. dplyr簡介
dplyr是R語言的數據分析包,類似於python中的pandas,能對dataframe類型的數據做很方便的數據處理和分析操作。最初我也很奇怪dplyr這個奇怪的名字,我查到其中一種解釋 - d代表dataframe - plyr是英文鉗子plier的諧音
dplyr如同R的大多數包,都是函數式編程,這點跟Python面向對象編程區別很大。優點是初學者比較容易接受這種函數式思維,有點類似於流水線,每個函數就是一個車間,多個車間共同完成一個生產(數據分析)任務。
而在dplyr中,就有一個管道符 %>% ,符號左側表示數據的輸入,右側表示下游數據處理環節。
2. 安裝並導入dplyr庫
pacman庫的p_load函數功能包含了
install.packages(“dplyr”)
library(dplyr)
該寫法更簡單易用
pacman::p_load("dplyr")
3. 讀取數據
#設置工作目錄
setwd("/Users/thunderhit/Desktop/dplyr_learn")
#導入csv數據
aapl <- read.csv('aapl.csv',
header=TRUE,
sep=',',
stringsAsFactors = FALSE) %>% as_tibble()
head(aapl)
Date | Open | High | Low | Close | Volume |
---|---|---|---|---|---|
<chr> | <dbl> | <dbl> | <dbl> | <dbl> | <int> |
7-Jul-17 | 142.90 | 144.75 | 142.90 | 144.18 | 19201712 |
6-Jul-17 | 143.02 | 143.50 | 142.41 | 142.73 | 24128782 |
5-Jul-17 | 143.69 | 144.79 | 142.72 | 144.09 | 21569557 |
3-Jul-17 | 144.88 | 145.30 | 143.10 | 143.50 | 14277848 |
30-Jun-17 | 144.45 | 144.96 | 143.78 | 144.02 | 23024107 |
29-Jun-17 | 144.71 | 145.13 | 142.28 | 143.68 | 31499368 |
查看數據類型
class(aapl)
'tbl_df'
'tbl'
'data.frame'
查看數據的字段
colnames(aapl)
'Date'
'Open'
'High'
'Low'
'Close'
'Volume'
查看記錄數、字段數
dim(aapl)
251
6
4. dplyr常用函數
4.1 Arrange
對appl數據按照字段Volume進行降序排序
arrange(aapl, -Volume)
Date | Open | High | Low | Close | Volume |
---|---|---|---|---|---|
<chr> | <dbl> | <dbl> | <dbl> | <dbl> | <int> |
14-Sep-16 | 108.73 | 113.03 | 108.60 | 111.77 | 112340318 |
1-Feb-17 | 127.03 | 130.49 | 127.01 | 128.75 | 111985040 |
27-Jul-16 | 104.26 | 104.35 | 102.75 | 102.95 | 92344820 |
15-Sep-16 | 113.86 | 115.73 | 113.49 | 115.57 | 90613177 |
16-Sep-16 | 115.12 | 116.13 | 114.04 | 114.92 | 79886911 |
12-Jun-17 | 145.74 | 146.09 | 142.51 | 145.42 | 72307330 |
我們也可以用管道符 %>% ,兩種寫法得到的運行結果是一致的,可能用久了會覺得管道符 %>% 可讀性更強,後面我們都會用 %>% 來寫代碼。
aapl %>% arrange(-Volume)
Date | Open | High | Low | Close | Volume |
---|---|---|---|---|---|
<chr> | <dbl> | <dbl> | <dbl> | <dbl> | <int> |
14-Sep-16 | 108.73 | 113.03 | 108.60 | 111.77 | 112340318 |
1-Feb-17 | 127.03 | 130.49 | 127.01 | 128.75 | 111985040 |
27-Jul-16 | 104.26 | 104.35 | 102.75 | 102.95 | 92344820 |
15-Sep-16 | 113.86 | 115.73 | 113.49 | 115.57 | 90613177 |
16-Sep-16 | 115.12 | 116.13 | 114.04 | 114.92 | 79886911 |
12-Jun-17 | 145.74 | 146.09 | 142.51 | 145.42 | 72307330 |
4.2 Select
選取 Date、Close和Volume三列
aapl %>% select(Date, Close, Volume)
Date | Close | Volume |
---|---|---|
<chr> | <dbl> | <int> |
7-Jul-17 | 144.18 | 19201712 |
6-Jul-17 | 142.73 | 24128782 |
5-Jul-17 | 144.09 | 21569557 |
3-Jul-17 | 143.50 | 14277848 |
30-Jun-17 | 144.02 | 23024107 |
29-Jun-17 | 143.68 | 31499368 |
只選取Date、Close和Volume三列,其實另外一種表達方式是“排除Open、High、Low,選擇剩下的字段的數據”。
aapl %>% select(-c("Open", "High", "Low"))
Date | Close | Volume |
---|---|---|
<chr> | <dbl> | <int> |
7-Jul-17 | 144.18 | 19201712 |
6-Jul-17 | 142.73 | 24128782 |
5-Jul-17 | 144.09 | 21569557 |
3-Jul-17 | 143.50 | 14277848 |
30-Jun-17 | 144.02 | 23024107 |
29-Jun-17 | 143.68 | 31499368 |
4.3 Filter
按照篩選條件選擇數據
#從數據中選擇appl股價大於150美元的交易數據
aapl %>% filter(Close>=150)
Date | Open | High | Low | Close | Volume |
---|---|---|---|---|---|
<chr> | <dbl> | <dbl> | <dbl> | <dbl> | <int> |
8-Jun-17 | 155.25 | 155.54 | 154.40 | 154.99 | 21250798 |
7-Jun-17 | 155.02 | 155.98 | 154.48 | 155.37 | 21069647 |
6-Jun-17 | 153.90 | 155.81 | 153.78 | 154.45 | 26624926 |
5-Jun-17 | 154.34 | 154.45 | 153.46 | 153.93 | 25331662 |
2-Jun-17 | 153.58 | 155.45 | 152.89 | 155.45 | 27770715 |
1-Jun-17 | 153.17 | 153.33 | 152.22 | 153.18 | 16404088 |
從數據中選擇appl - 股價大於150美元 且 收盤價大於開盤價 的交易數據
aapl %>% filter((Close>=150) & (Close>Open))
Date | Open | High | Low | Close | Volume |
---|---|---|---|---|---|
<chr> | <dbl> | <dbl> | <dbl> | <dbl> | <int> |
7-Jun-17 | 155.02 | 155.98 | 154.48 | 155.37 | 21069647 |
6-Jun-17 | 153.90 | 155.81 | 153.78 | 154.45 | 26624926 |
2-Jun-17 | 153.58 | 155.45 | 152.89 | 155.45 | 27770715 |
1-Jun-17 | 153.17 | 153.33 | 152.22 | 153.18 | 16404088 |
30-May-17 | 153.42 | 154.43 | 153.33 | 153.67 | 20126851 |
25-May-17 | 153.73 | 154.35 | 153.03 | 153.87 | 19235598 |
18-May-17 | 151.27 | 153.34 | 151.13 | 152.54 | 33568215 |
12-May-17 | 154.70 | 156.42 | 154.67 | 156.10 | 32527017 |
11-May-17 | 152.45 | 154.07 | 152.31 | 153.95 | 27255058 |
9-May-17 | 153.87 | 154.88 | 153.45 | 153.99 | 39130363 |
8-May-17 | 149.03 | 153.70 | 149.03 | 153.01 | 48752413 |
4.4 Mutate
將現有的字段經過計算後生成新字段。
#將最好價High減去最低價Low的結果定義爲maxDif,並取log
aapl %>% mutate(maxDif = High-Low,
log_maxDif=log(maxDif))
Date | Open | High | Low | Close | Volume | maxDif | log_maxDif |
---|---|---|---|---|---|---|---|
<chr> | <dbl> | <dbl> | <dbl> | <dbl> | <int> | <dbl> | <dbl> |
7-Jul-17 | 142.90 | 144.75 | 142.90 | 144.18 | 19201712 | 1.85 | 0.6151856 |
6-Jul-17 | 143.02 | 143.50 | 142.41 | 142.73 | 24128782 | 1.09 | 0.0861777 |
5-Jul-17 | 143.69 | 144.79 | 142.72 | 144.09 | 21569557 | 2.07 | 0.7275486 |
3-Jul-17 | 144.88 | 145.30 | 143.10 | 143.50 | 14277848 | 2.20 | 0.7884574 |
30-Jun-17 | 144.45 | 144.96 | 143.78 | 144.02 | 23024107 | 1.18 | 0.1655144 |
29-Jun-17 | 144.71 | 145.13 | 142.28 | 143.68 | 31499368 | 2.85 | 1.0473190 |
得到記錄的位置(行數)
aapl %>% mutate(n=row_number())
Date | Open | High | Low | Close | Volume | n |
---|---|---|---|---|---|---|
<chr> | <dbl> | <dbl> | <dbl> | <dbl> | <int> | <int> |
7-Jul-17 | 142.90 | 144.75 | 142.90 | 144.18 | 19201712 | 1 |
6-Jul-17 | 143.02 | 143.50 | 142.41 | 142.73 | 24128782 | 2 |
5-Jul-17 | 143.69 | 144.79 | 142.72 | 144.09 | 21569557 | 3 |
3-Jul-17 | 144.88 | 145.30 | 143.10 | 143.50 | 14277848 | 4 |
30-Jun-17 | 144.45 | 144.96 | 143.78 | 144.02 | 23024107 | 5 |
29-Jun-17 | 144.71 | 145.13 | 142.28 | 143.68 | 31499368 | 6 |
4.5 Group_By
對資料進行分組,這裏導入新的 數據集 weather
#導入csv數據
weather <- read.csv('weather.csv',
header=TRUE,
sep=',',
stringsAsFactors = FALSE) %>% as_tibble()
weather
Date | city | temperature | windspeed | event |
---|---|---|---|---|
<chr> | <chr> | <int> | <int> | <chr> |
1/1/2017 | new york | 32 | 6 | Rain |
1/1/2017 | mumbai | 90 | 5 | Sunny |
1/1/2017 | paris | 45 | 20 | Sunny |
1/2/2017 | new york | 36 | 7 | Sunny |
1/2/2017 | mumbai | 85 | 12 | Fog |
1/2/2017 | paris | 50 | 13 | Cloudy |
按照城市分組
weather %>% group_by(city)
Date | city | temperature | windspeed | event |
---|---|---|---|---|
<chr> | <chr> | <int> | <int> | <chr> |
1/1/2017 | new york | 32 | 6 | Rain |
1/1/2017 | mumbai | 90 | 5 | Sunny |
1/1/2017 | paris | 45 | 20 | Sunny |
1/2/2017 | new york | 36 | 7 | Sunny |
1/2/2017 | mumbai | 85 | 12 | Fog |
1/2/2017 | paris | 50 | 13 | Cloudy |
爲了讓大家看到分組的功效,咱們按照城市分別計算平均溫度
weather %>% group_by(city) %>% summarise(mean_temperature = mean(temperature))
`summarise()` ungrouping output (override with `.groups` argument)
city | mean_temperature |
---|---|
<chr> | <dbl> |
mumbai | 87.5 |
new york | 34.0 |
paris | 47.5 |
weather %>% summarise(mean_temperature = mean(temperature))
mean_temperature |
---|
<dbl> |
56.33333 |
往期文章小案例: Pandas的apply方法
Python語法快速入門
Python網絡爬蟲與文本數據分析
讀完本文你就瞭解什麼是文本分析
綜述:文本分析在市場營銷研究中的應用
從記者的Twitter關注看他們稿件的黨派傾向?
Pandas時間序列數據操作
70G上市公司定期報告數據集
文本數據清洗之正則表達式
shreport庫: 批量下載上海證券交易所上市公司年報
Numpy和Pandas性能改善的方法和技巧
漂亮~pandas可以無縫銜接Bokeh
YelpDaset: 酒店管理類數據集10+G
半個小時學會Markdown標記語法
後臺回覆關鍵詞【dplyr實操】,可獲得測試數據及代碼