使用dplyr包處理數(shù)據(jù)-學(xué)習(xí)筆記

# dplyr 基礎(chǔ)

  • 按值篩選觀測(cè) filter()

  • 對(duì)行進(jìn)行重新排序 arrange()

  • 按名稱選取列變量 select()

  • 使用現(xiàn)有變量的函數(shù)創(chuàng)建新變量 mutate()

  • 將多個(gè)值總結(jié)為一個(gè)摘要統(tǒng)計(jì)量 summarize()

  • 以上函數(shù)與group_by()函數(shù)連用,group_by()可以改變函數(shù)的作用范圍;讓其從整個(gè)數(shù)據(jù)集上的操作變?yōu)樵诿總€(gè)分組上的分別操作 group_by()

# 數(shù)據(jù)
nycflights13::flights, 2013年從紐約出發(fā)的336 776次航班的信息。

> install.packages("nycflights13")
> library(dplyr)
> library(nycflights13)
> head(flights)
# A tibble: 6 x 19
   year month   day dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier flight tailnum origin dest  air_time
  <int> <int> <int>    <int>          <int>     <dbl>    <int>          <int>     <dbl> <chr>    <int> <chr>   <chr>  <chr>    <dbl>
1  2013     1     1      517            515         2      830            819        11 UA        1545 N14228  EWR    IAH        227
2  2013     1     1      533            529         4      850            830        20 UA        1714 N24211  LGA    IAH        227
# ... with 4 more variables: distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>

# 使用filter()篩選行
filter(.data, ...)
.data是數(shù)據(jù)框;
...:后面可以是一個(gè)或多個(gè)參數(shù),但必須是邏輯值,從而保證為TRUE的行才保留。

#篩選1月1日的航班
> filter(flights,month==1,day==1)
# A tibble: 842 x 19
    year month   day dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier flight tailnum origin dest  air_time
   <int> <int> <int>    <int>          <int>     <dbl>    <int>          <int>     <dbl> <chr>    <int> <chr>   <chr>  <chr>    <dbl>
 1  2013     1     1      517            515         2      830            819        11 UA        1545 N14228  EWR    IAH        227

## 比較運(yùn)算符
>, >=, <, <=, !=, ==
浮點(diǎn)數(shù)相等使用near()代替==

## 邏輯運(yùn)算符
& 表示“與”, | 表示“或”, !表示“非”, xor(x,y) 表示“異或”

> x <- c(T,T,F)
> y <- c(F,T,F)
> xor(x,y)
[1]  TRUE FALSE FALSE

## 缺失值
NA:not available, 不可用;
is.NA()
# 使用arrange()排列行
arrange(.data, ...)
.data:數(shù)據(jù)框
...:變量名,多個(gè)變量名使用逗號(hào)隔開; arrange()將根據(jù)這些列對(duì)數(shù)據(jù)框進(jìn)行排序;

> arrange(flights,year,month,day)
# A tibble: 336,776 x 19
    year month   day dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier flight tailnum origin dest  air_time
   <int> <int> <int>    <int>          <int>     <dbl>    <int>          <int>     <dbl> <chr>    <int> <chr>   <chr>  <chr>    <dbl>
 1  2013     1     1      517            515         2      830            819        11 UA        1545 N14228  EWR    IAH        227
 2  2013     1     1      533            529         4      850            830        20 UA        1714 N24211  LGA    IAH        227

使用desc() 可以按列進(jìn)行降序排序:

> arrange(flights,desc(arr_delay))

# 使用select()選擇列
select(.data, ...):keeps only the variables you mention

> select(flights,year,month,day)
> select(flights,year,month,day)
# A tibble: 336,776 x 3
    year month   day
   <int> <int> <int>
 1  2013     1     1
 2  2013     1     1
 3  2013     1     1
 4  2013     1     1
 5  2013     1     1
 6  2013     1     1
 7  2013     1     1
 8  2013     1     1
 9  2013     1     1
10  2013     1     1
# ... with 336,766 more rows
> select(flights,year:day)
> select(flights,-(year:day))  #刪除列

select()函數(shù)可以結(jié)合一些輔助函數(shù)使用:
start_with("abc"): 匹配“abc”開頭
ends_with(): 匹配“xyz” 結(jié)尾的名稱
contains("ijk"): 匹配包含“ijk”的名稱
mathces("(.)\1"): 匹配名稱中有重復(fù)字符的變量;(.)匹配單個(gè)字符;\\1指定第一個(gè)子匹配項(xiàng), 第二個(gè)匹配模式也是(.)。
num_range("x",1:3): 匹配x1、x2和x3
rename(): 可以對(duì)數(shù)據(jù)框中變量名重命名。

> rename(flights,tail_num = tailnum)
# A tibble: 336,776 x 19
    year month   day dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier flight
   <int> <int> <int>    <int>          <int>     <dbl>    <int>          <int>     <dbl> <chr>    <int>
 1  2013     1     1      517            515         2      830            819        11 UA        1545
 2  2013     1     1      533            529         4      850            830        20 UA        1714
# ... with 336,766 more rows, and 8 more variables: tail_num <chr>, origin <chr>, dest <chr>,
#   air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>

# 使用mutate添加新變量
mutate()總是將新列添加在數(shù)據(jù)集的最后

> head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          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.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

>mutate(iris,newCol1=iris$Sepal.Length*2,newCol2=paste(iris$Species,iris$Sepal.Length,sep = ""))
    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species newCol1       newCol2
1            5.1         3.5          1.4         0.2     setosa    10.2     setosa5.1
2            4.9         3.0          1.4         0.2     setosa     9.8     setosa4.9
3            4.7         3.2          1.3         0.2     setosa     9.4     setosa4.7
4            4.6         3.1          1.5         0.2     setosa     9.2     setosa4.6
5            5.0         3.6          1.4         0.2     setosa    10.0       setosa5

transmute()只保留新變量

>transmute(iris,newCol1=iris$Sepal.Length*2,newCol2=paste(iris$Species,iris$Sepal.Length,sep = ""))
    newCol1       newCol2
1      10.2     setosa5.1
2       9.8     setosa4.9
3       9.4     setosa4.7
4       9.2     setosa4.6
5      10.0       setosa5

# 使用summarize()進(jìn)行分組摘要

group_by()可以將分析單位從整個(gè)數(shù)據(jù)集改為單個(gè)分組
ungroup()可以取消分組

> bySpecies<-group_by(iris,Species)
> summarize(bySpecies, meanCol=mean(iris$Sepal.Length,na.rm = T))
# A tibble: 3 x 2
  Species    meanCol
  <fct>        <dbl>
1 setosa        5.84
2 versicolor    5.84
3 virginica     5.84
> by_day<-group_by(flights,year,month,day)  #flights中year,month,day各水平一一組合

使用管道操作: %>%,輸入數(shù)據(jù)或者計(jì)算結(jié)果通過%>%直接傳遞給下一步操作

by_dest<-group_by(flights,dest)
delay<-summarize(by_dest,
                 count=n(),  #n()是一個(gè)計(jì)數(shù)函數(shù)
                 dist=mean(distance,na.rm=T),
                 delay=mean(arr_delay,na.rm = T))

delay<-filter(delay,count>20,dest != "HNL")


by_dest<-flights %>%
  group_by(dest)%>%
  summarize(
    count=n(),
    dist=mean(distance,na.rm=T),
    delay=mean(arr_delay,na.rm = T)
    )%>%
  filter(count>20,dest != "HNL")

參考:
R數(shù)據(jù)科學(xué)

最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • MYSQL 基礎(chǔ)知識(shí) 1 MySQL數(shù)據(jù)庫概要 2 簡(jiǎn)單MySQL環(huán)境 3 數(shù)據(jù)的存儲(chǔ)和獲取 4 MySQL基本操...
    Kingtester閱讀 8,069評(píng)論 5 115
  • 1. 數(shù)據(jù)操作:dplyr包應(yīng)用 dplyr包是為數(shù)據(jù)分析提供了一系列快捷有效的操作,其中有五個(gè)關(guān)鍵函數(shù)基本可以解...
    100gle閱讀 3,707評(píng)論 0 7
  • ORA-00001: 違反唯一約束條件 (.) 錯(cuò)誤說明:當(dāng)在唯一索引所對(duì)應(yīng)的列上鍵入重復(fù)值時(shí),會(huì)觸發(fā)此異常。 O...
    我想起個(gè)好名字閱讀 6,010評(píng)論 0 9
  • 不斷更新,豐富自己的知識(shí),訓(xùn)練自己的邏輯思維,強(qiáng)化執(zhí)行能力,而這一切都是需要時(shí)間來沉淀和累積的,所有的良好的習(xí)慣都...
    Fineyoga林丹閱讀 277評(píng)論 0 1
  • 魯迅的《吶喊》、《彷徨》,從小到大,我想也讀了不下四五回了吧,每次讀都是沉重的,只覺得這世上除了這魯迅先生,沒人是...
    蛋撻米糊閱讀 177評(píng)論 0 0

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