tidyverse是為數(shù)據(jù)科學(xué)設(shè)計的R軟件包浊仆,它包含(ggplot2张弛、dplyr冗茸、tidyr、stringr、magrittr、tibble)等一系列熱門軟件包撵渡,學(xué)好tidyverse的使用可也讓你站上另一個高度,從而高效的處理數(shù)據(jù)死嗦,因此本文檔不僅僅做一些案例介紹趋距,而是希望以較為正確的學(xué)習(xí)方法來介紹R語言,使大家少走彎路越除,快速入門掌握R語言节腐。
1. 安裝tidyverse
install.packages("tidyverse")
library(tidyverse)
> library(tidyverse)
─ Attaching packages ─────────── tidyverse 1.3.0 ─
? ggplot2 3.3.2 ? purrr 0.3.4
? tibble 3.0.4 ? dplyr 1.0.2
? tidyr 1.1.2 ? stringr 1.4.0
? readr 1.4.0 ? forcats 0.5.0
─ Conflicts ──────────── tidyverse_conflicts() ─
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
2. iris數(shù)據(jù)集
我們將使用iris(鳶尾花)數(shù)據(jù)集,因此花一點時間來熟悉一下它摘盆,加載ggplot2軟件包時翼雀,可以使用此內(nèi)置數(shù)據(jù)集。加載tidyverse軟件包將自動加載ggplot2孩擂。
View(iris) #可以像excel一樣查看數(shù)據(jù)
attributes(iris) #查看數(shù)據(jù)屬性
> attributes(iris)
$names
[1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
[5] "Species"
$class
[1] "data.frame"
$row.names
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
[17] 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
[33] 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
[49] 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
[65] 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
[81] 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
[97] 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
[113] 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
[129] 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
[145] 145 146 147 148 149 150
可以看到數(shù)據(jù)有5列狼渊,150行,數(shù)據(jù)類型為數(shù)據(jù)框类垦;分別表示Sepal.Length
(花萼長度)狈邑,Sepal.Width
(花萼寬度)、Petal.Length
(花瓣長度)蚤认,Petal.Width
(花瓣寬度)米苹、Species
(花的類型),其中花有3種類型(setosa砰琢、versicolor蘸嘶、virginica)
上面介紹了iris
數(shù)據(jù)集良瞧,接著我們開始一些基礎(chǔ)的數(shù)據(jù)操作
3.使用dplyr對數(shù)據(jù)進行操作
3.1 select(按名稱選取列)
select(iris,Sepal.Length,Petal.Length,Species)
#為了查看方便也可以只查看前6行
head(select(iris,Sepal.Length,Petal.Length,Species))
將篩選出來的結(jié)果通過賦值操作符<-
給一個變量,如下所示
p <- select(iris,Sepal.Length,Petal.Length,Species)
接著用此數(shù)據(jù)進行一個最基礎(chǔ)的可視化:
關(guān)于ggplot2的原理可參考:http://www.reibang.com/p/4da5a941e8b5
ggplot(p,aes(Sepal.Length,Petal.Length))+
geom_point(aes(color=Species),size=2)
select選擇2列之間的所有列
select(iris,Sepal.Length:Petal.Length))
select選擇不在2列之間的所有列
select(iris,-(Sepal.Length:Petal.Length))
select改變列的順序
#select()與everythin()函數(shù)結(jié)合使用可以改變列的順序
select(iris,Species,Petal.Width,Sepal.Width,
Sepal.Length,Petal.Length,everything())
3.2 filter(按值篩選行)
filter(iris,Sepal.Length >=5,Petal.Length >=2)
p1 <- filter(iris,Sepal.Length >=5,Petal.Length >=2)
ggplot(p1,aes(Sepal.Length,Petal.Length))+
geom_point(aes(color=Species),size=2)
R中的比較運算符:>
亏较、>=
莺褒、<
、<=
雪情、!=
(不等于)、==
(等于)
R中的邏輯運算符:&
表示"與”
你辣,|
表示“或”
巡通,!
表示“非”
3.3 arrange(改變行順序)
#根據(jù)Petal.Width列的數(shù)據(jù)進行排序,默認為升序
arrange(iris,Petal.Width)
#desc()可以按列進行降序排序:
arrange(iris,desc(Petal.Width)))
3.5 rename(更改列名稱)
#新名稱在前舍哄,原始名稱在后
rename(iris,length=Sepal.Length)
rename(iris,replace=c("Sepal.Length"="length"))
3.6 mutate(添加新列)
mutate(iris,group ="A",Length=10)
3.7 summarize(進行分組摘要)
summarize它可以將數(shù)據(jù)框折疊成一行
summarize(iris,mean(Sepal.Length),
sd(Sepal.Length))
3.8 group_by()
group_by可以將分析單位從整個數(shù)據(jù)集更改為單個分組
iris %>% group_by(Species) %>%
summarize(m = mean(Sepal.Length,na.rm=T))
# na.rm=T 表示移除缺失數(shù)據(jù)
`summarise()` ungrouping output (override with `.groups` argument)
# A tibble: 3 x 2
Species m
<fct> <dbl>
1 setosa 5.01
2 versicolor 5.94
3 virginica 6.59
3.9 %>%(管道)
利用管道可以簡化代碼,提高代碼閱讀流暢性:
p1 <- filter(iris,Sepal.Length >=5,Petal.Length >=2)
p2 <- group_by(p1,Species)
p3 <- filter(p2,Species=="virginica")
ggplot(p3,aes(Sepal.Length,Petal.Length))+
geom_point(aes(color=Species),size=2)
iris %>% filter(Sepal.Length >=5,Petal.Length >=2) %>%
group_by(Species) %>% filter(Species=="virginica") %>%
ggplot(aes(Sepal.Length,Petal.Length))+
geom_point(aes(color=Species),size=2)
這2段代碼結(jié)果相同,可以明顯看到使用了%>%
減少了中間變量比伏,提高了代碼的可閱讀性
iris %>% filter(.,Sepal.Length >=5,Petal.Length >=2)
管道的原理就是將%>%
左邊的變量傳遞到右邊的.
處沼侣,通常在正式書寫時可省略.
3.10 count() 計算每組值的次數(shù)
iris %>% count(Species)
tidyverse中
還有很多的有用的函數(shù),但是上面所述的均為在數(shù)據(jù)處理中使用頻率最高的函數(shù)蟆沫,到此為止我們已經(jīng)介紹了畫圖原理籽暇,及一系列數(shù)據(jù)處理函數(shù),下面就可以通過一系列可視化案例來不斷加深學(xué)習(xí)