R語(yǔ)言初步-數(shù)據(jù)轉(zhuǎn)換-6.summarise()函數(shù)的綜合運(yùn)用

R語(yǔ)言學(xué)習(xí)筆記總結(jié)

R語(yǔ)言初步-用dplyr進(jìn)行數(shù)據(jù)轉(zhuǎn)換

install.packages("tidyverse")
install.packages("nycflights13")#仍然記得要先安裝
library(nycflights13)#航班信息文件
library(tidyverse)

?flights#查看數(shù)據(jù)信息的說(shuō)明書(shū)
flights#查看航班信息

5、其他常用的摘要函數(shù)

之前使用了均值、求和和計(jì)數(shù)

5.1、位置度量:median()函數(shù)

median()用法和mean()類似,只不過(guò)是中位數(shù)而已

Not_cancelled <- flights %>%
  filter(!is.na(dep_delay),!is.na(arr_delay))

Not_cancelled %>%
  group_by(year,month,day)%>%
  summarise(
    #平均延誤時(shí)間
    avg_delay1=mean(arr_delay),
    #平均正延誤時(shí)間
    avg_delay2=mean(arr_delay[arr_delay>0]),
  )

#運(yùn)行:
A tibble: 365 x 5
# Groups:   year, month [12]
    year month   day avg_delay1 avg_delay2
   <int> <int> <int>      <dbl>      <dbl>
 1  2013     1     1     12.7         32.5
 2  2013     1     2     12.7         32.0
 3  2013     1     3      5.73        27.7
 4  2013     1     4     -1.93        28.3
 5  2013     1     5     -1.53        22.6
 6  2013     1     6      4.24        24.4
 7  2013     1     7     -4.95        27.8
 8  2013     1     8     -3.23        20.8
 9  2013     1     9     -0.264       25.6
10  2013     1    10     -5.90        27.3
# ... with 355 more rows

5.2、分散程度度量:sd()、IQR()、mad()函數(shù)

  • sd():標(biāo)準(zhǔn)誤差函數(shù):standard deviation,分散程度的標(biāo)準(zhǔn)度量方式
  • IQR():四分位距
  • mad():絕對(duì)中位差

注:mad()與IQR()基本等價(jià),但是IQR()更適合有離群點(diǎn)的情況。


Not_cancelled %>%
  group_by(dest)%>%
  summarise(
    distance_sd=sd(distance))%>%  #計(jì)算distance列的標(biāo)準(zhǔn)誤差
    arrange(desc(distance_sd)             #降序排序此行
  )                           

#運(yùn)行:
A tibble: 104 x 2
   dest  distance_sd
   <chr>       <dbl>
 1 EGE         10.5 
 2 SAN         10.4 
 3 SFO         10.2 
 4 HNL         10.0 
 5 SEA          9.98
 6 LAS          9.91
 7 PDX          9.87
 8 PHX          9.86
 9 LAX          9.66
10 IND          9.46
# ... with 94 more rows

5.3、秩的度量:min()、quantile()、max()函數(shù)

quantile():分位數(shù)函數(shù),是中位數(shù)函數(shù)的拓展
使用說(shuō)明:quantile(x,0.25)是指將x按從小到大順序排列,找到大于前25%,小于后75%的值。

#每天最早和最晚的航班是是什么時(shí)候:

Not_cancelled %>%
  group_by(year,month,day)%>%  #先按時(shí)間分組
  summarise(
    first=min(dep_time),  #最小值
    last=max(dep_time)    #最大值
    )

#運(yùn)行:
A tibble: 365 x 5
# Groups:   year, month [12]
    year month   day first  last
   <int> <int> <int> <int> <int>
 1  2013     1     1   517  2356
 2  2013     1     2    42  2354
 3  2013     1     3    32  2349
 4  2013     1     4    25  2358
 5  2013     1     5    14  2357
 6  2013     1     6    16  2355
 7  2013     1     7    49  2359
 8  2013     1     8   454  2351
 9  2013     1     9     2  2252
10  2013     1    10     3  2320
# ... with 355 more rows

5.4、定位度量:first()、nth()、last()函數(shù)

這三個(gè)函數(shù)的作用相當(dāng)于x[1]、x[2]、x[length(x)]
通過(guò)此函數(shù)也可以找出最早和最晚出發(fā)的航班


Not_cancelled %>%
  group_by(year,month,day)%>%  
  summarise(
    first_dep=first(dep_time), 
    last_dep=last(dep_time)    
  )

#運(yùn)行:
# A tibble: 365 x 5
# Groups:   year, month [12]
    year month   day first_dep last_dep
   <int> <int> <int>     <int>    <int>
 1  2013     1     1       517     2356
 2  2013     1     2        42     2354
 3  2013     1     3        32     2349
 4  2013     1     4        25     2358
 5  2013     1     5        14     2357
 6  2013     1     6        16     2355
 7  2013     1     7        49     2359
 8  2013     1     8       454     2351
 9  2013     1     9         2     2252
10  2013     1    10         3     2320
# ... with 355 more rows

5.5、計(jì)數(shù)n(),count()

n():不需要任何參數(shù),返回當(dāng)前分組的大小
sum(!is.na(x)):計(jì)算非缺失值的數(shù)量
n_distinct(x):計(jì)算唯一值的數(shù)量
count()函數(shù):用于只需要計(jì)數(shù)的情況

例如:
計(jì)算哪個(gè)目的地有最多的航空公司?

Not_cancelled %>%
  group_by(dest)%>%  
  summarise(
    carriers=n_distinct(carrier))%>%
  arrange(desc(carriers))

#運(yùn)行:
A tibble: 104 x 2
   dest  carriers
   <chr>    <int>
 1 ATL          7
 2 BOS          7
 3 CLT          7
 4 ORD          7
 5 TPA          7
 6 AUS          6
 7 DCA          6
 8 DTW          6
 9 IAD          6
10 MSP          6
# ... with 94 more rows

count()函數(shù)用法舉例:計(jì)算目的地不同的飛機(jī)數(shù)量

Not_cancelled %>%
  count(dest)

#運(yùn)行:
#A tibble: 104 x 2
   dest      n
   <chr> <int>
 1 ABQ     254
 2 ACK     264
 3 ALB     418
 4 ANC       8
 5 ATL   16837
 6 AUS    2411
 7 AVL     261
 8 BDL     412
 9 BGR     358
10 BHM     269
# ... with 94 more rows 

count()函數(shù)中可以添加加權(quán)變量,例如distance,用于計(jì)算飛機(jī)飛行里程(相當(dāng)于求和)

Not_cancelled %>%
  count(tailnum,wt=distance)

#運(yùn)行:
# A tibble: 4,037 x 2
   tailnum      n
   <chr>    <dbl>
 1 D942DN    3418
 2 N0EGMQ  239143
 3 N10156  109664
 4 N102UW   25722
 5 N103US   24619
 6 N104UW   24616
 7 N10575  139903
 8 N105UW   23618
 9 N107US   21677
10 N108UW   32070
# ... with 4,027 more rows

5.6、邏輯值的計(jì)數(shù)和比例

當(dāng)需要用數(shù)值表示結(jié)果,TRUE=1,F(xiàn)ALSE=0。
sum():可以找出TRUE的數(shù)量
mean():可以找出比例

以下一例:找出出發(fā)時(shí)間小于5:00的航班總數(shù)

Not_cancelled %>%
  group_by(year,month,day)%>%  
  summarise(
    n_nearly=sum(dep_time<500) #出發(fā)時(shí)間小于5:00的航班總數(shù) 
  )

#運(yùn)行:
# A tibble: 365 x 4
# Groups:   year, month [12]
    year month   day n_nearly
   <int> <int> <int>    <int>
 1  2013     1     1        0
 2  2013     1     2        3
 3  2013     1     3        4
 4  2013     1     4        3
 5  2013     1     5        3
 6  2013     1     6        2
 7  2013     1     7        2
 8  2013     1     8        1
 9  2013     1     9        3
10  2013     1    10        3
# ... with 355 more rows

sum(dep_time<500)換成count(dep_time<500)是沒(méi)有用的,sum相當(dāng)于計(jì)算了返回值1,而dep_time<50這樣的邏輯表達(dá),count()函數(shù)是不支持的,其中牽扯到數(shù)據(jù)的邏輯。

以下一例:找出延誤超過(guò)一小時(shí)的航班比例

Not_cancelled %>%
     group_by(year,month,day)%>%  
     summarise(
         hour_perc=mean(arr_delay>60)  #延誤超過(guò)一小時(shí)的航班
       )

#運(yùn)行:
# A tibble: 365 x 4
# Groups:   year, month [12]
    year month   day hour_perc
   <int> <int> <int>     <dbl>
 1  2013     1     1    0.0722
 2  2013     1     2    0.0851
 3  2013     1     3    0.0567
 4  2013     1     4    0.0396
 5  2013     1     5    0.0349
 6  2013     1     6    0.0470
 7  2013     1     7    0.0333
 8  2013     1     8    0.0213
 9  2013     1     9    0.0202
10  2013     1    10    0.0183
# ... with 355 more rows

                                    學(xué)習(xí)R語(yǔ)言真的好快樂(lè)哈哈
生活也不過(guò)如此.jpg
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容