# 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é)