tidyverse是R的一个包,可以让数据处理变得更加简洁。其中自带八个常用的包ggplot2/ tibble/ tidyr/ readr/ purrr/ dplyr/ stringr/forcat,可以实现常见的数据处理和可视化操作。
数据框排序
sort
base包的函数。给向量排序,返回从小到大的排序,参数是decreasing,可以是T或F。
> testSepal.Length Sepal.Width Petal.Length Petal.Width Species1 5.1 3.5 1.4 0.2 setosa2 4.9 3.0 1.4 0.2 setosa3 7.0 3.2 4.7 1.4 versicolor4 6.4 3.2 4.5 1.5 versicolor5 6.3 3.3 6.0 2.5 virginica6 5.8 2.7 5.1 1.9 virginica## 向量排序(升序)> sort(test$Sepal.Length)[1] 4.9 5.1 5.8 6.3 6.4 7.0## 向量排序(降序)> sort(test$Sepal.Length,decreasing = T)[1] 7.0 6.4 6.3 5.8 5.1 4.9
order
base包的函数。可以给向量或者数据框排序,返回sort排序的下标,利用此逻辑给数据框排序。
> testSepal.Length Sepal.Width Petal.Length Petal.Width Species1 5.1 3.5 1.4 0.2 setosa2 4.9 3.0 1.4 0.2 setosa3 7.0 3.2 4.7 1.4 versicolor4 6.4 3.2 4.5 1.5 versicolor5 6.3 3.3 6.0 2.5 virginica6 5.8 2.7 5.1 1.9 virginica## 给数据框排序(正序)> test[order(test$Sepal.Length),]Sepal.Length Sepal.Width Petal.Length Petal.Width Species2 4.9 3.0 1.4 0.2 setosa1 5.1 3.5 1.4 0.2 setosa6 5.8 2.7 5.1 1.9 virginica5 6.3 3.3 6.0 2.5 virginica4 6.4 3.2 4.5 1.5 versicolor3 7.0 3.2 4.7 1.4 versicolor## 给数据框排序(降序)> test[order(test$Sepal.Length,decreasing = T),]Sepal.Length Sepal.Width Petal.Length Petal.Width Species3 7.0 3.2 4.7 1.4 versicolor4 6.4 3.2 4.5 1.5 versicolor5 6.3 3.3 6.0 2.5 virginica6 5.8 2.7 5.1 1.9 virginica1 5.1 3.5 1.4 0.2 setosa2 4.9 3.0 1.4 0.2 setosa
arrange
是tidyverse包的函数,可以实现简单的代码排序,多个条件排序等操作。
> testSepal.Length Sepal.Width Petal.Length Petal.Width Species1 5.1 3.5 1.4 0.2 setosa2 4.9 3.0 1.4 0.2 setosa3 7.0 3.2 4.7 1.4 versicolor4 6.4 3.2 4.5 1.5 versicolor5 6.3 3.3 6.0 2.5 virginica6 5.8 2.7 5.1 1.9 virginica## 数据框排序(正序)> library(dplyr)> arrange(test, Sepal.Length)Sepal.Length Sepal.Width Petal.Length Petal.Width Species1 4.9 3.0 1.4 0.2 setosa2 5.1 3.5 1.4 0.2 setosa3 5.8 2.7 5.1 1.9 virginica4 6.3 3.3 6.0 2.5 virginica5 6.4 3.2 4.5 1.5 versicolor6 7.0 3.2 4.7 1.4 versicolor## 数据框排序(降序)> arrange(test, desc(Sepal.Length))Sepal.Length Sepal.Width Petal.Length Petal.Width Species1 7.0 3.2 4.7 1.4 versicolor2 6.4 3.2 4.5 1.5 versicolor3 6.3 3.3 6.0 2.5 virginica4 5.8 2.7 5.1 1.9 virginica5 5.1 3.5 1.4 0.2 setosa6 4.9 3.0 1.4 0.2 setosa## 多条件排序(先Sepal.Width降序,再Sepal.Length升序)> arrange(test, desc(Sepal.Width),Sepal.Length)Sepal.Length Sepal.Width Petal.Length Petal.Width Species1 5.1 3.5 1.4 0.2 setosa2 6.3 3.3 6.0 2.5 virginica3 6.4 3.2 4.5 1.5 versicolor4 7.0 3.2 4.7 1.4 versicolor5 4.9 3.0 1.4 0.2 setosa6 5.8 2.7 5.1 1.9 virginica
来自tidyr的几个函数
mutate
增加一列
test = mutate(test, new = Sepal.Length Sepal.Width)
*new代表新列名。
> testSepal.Length Sepal.Width Petal.Length Petal.Width Species1 5.1 3.5 1.4 0.2 setosa2 4.9 3.0 1.4 0.2 setosa3 7.0 3.2 4.7 1.4 versicolor4 6.4 3.2 4.5 1.5 versicolor5 6.3 3.3 6.0 2.5 virginica6 5.8 2.7 5.1 1.9 virginica> test = mutate(test, new = Sepal.Length * Sepal.Width)> testSepal.Length Sepal.Width Petal.Length Petal.Width Species new1 5.1 3.5 1.4 0.2 setosa 17.852 4.9 3.0 1.4 0.2 setosa 14.703 7.0 3.2 4.7 1.4 versicolor 22.404 6.4 3.2 4.5 1.5 versicolor 20.485 6.3 3.3 6.0 2.5 virginica 20.796 5.8 2.7 5.1 1.9 virginica 15.66## 简化代码> testSepal.Length Sepal.Width Petal.Length Petal.Width Species new happy1 5.1 3.5 1.4 0.2 setosa 17.85 17.852 4.9 3.0 1.4 0.2 setosa 14.70 14.703 7.0 3.2 4.7 1.4 versicolor 22.40 22.404 6.4 3.2 4.5 1.5 versicolor 20.48 20.485 6.3 3.3 6.0 2.5 virginica 20.79 20.796 5.8 2.7 5.1 1.9 virginica 15.66 15.66> test$new2 = test$Sepal.Length * test$Sepal.Width> testSepal.Length Sepal.Width Petal.Length Petal.Width Species new happy new21 5.1 3.5 1.4 0.2 setosa 17.85 17.85 17.852 4.9 3.0 1.4 0.2 setosa 14.70 14.70 14.703 7.0 3.2 4.7 1.4 versicolor 22.40 22.40 22.404 6.4 3.2 4.5 1.5 versicolor 20.48 20.48 20.485 6.3 3.3 6.0 2.5 virginica 20.79 20.79 20.796 5.8 2.7 5.1 1.9 virginica 15.66 15.66 15.66
select
选择某一列或者某几列。
> testSepal.Length Sepal.Width Petal.Length Petal.Width Species new happy new21 5.1 3.5 1.4 0.2 setosa 17.85 17.85 17.852 4.9 3.0 1.4 0.2 setosa 14.70 14.70 14.703 7.0 3.2 4.7 1.4 versicolor 22.40 22.40 22.404 6.4 3.2 4.5 1.5 versicolor 20.48 20.48 20.485 6.3 3.3 6.0 2.5 virginica 20.79 20.79 20.796 5.8 2.7 5.1 1.9 virginica 15.66 15.66 15.66## 选择某一列> select(test, Sepal.Length)Sepal.Length1 5.12 4.93 7.04 6.45 6.36 5.8## 选择某几列> select(test, starts_with("n"))new new21 17.85 17.852 14.70 14.703 22.40 22.404 20.48 20.485 20.79 20.796 15.66 15.66
filter
选择某一行或者某几行。
> testSepal.Length Sepal.Width Petal.Length Petal.Width Species new happy new21 5.1 3.5 1.4 0.2 setosa 17.85 17.85 17.852 4.9 3.0 1.4 0.2 setosa 14.70 14.70 14.703 7.0 3.2 4.7 1.4 versicolor 22.40 22.40 22.404 6.4 3.2 4.5 1.5 versicolor 20.48 20.48 20.485 6.3 3.3 6.0 2.5 virginica 20.79 20.79 20.796 5.8 2.7 5.1 1.9 virginica 15.66 15.66 15.66## 选择一列> filter(test, Sepal.Length == 6.4)Sepal.Length Sepal.Width Petal.Length Petal.Width Species new happy new21 6.4 3.2 4.5 1.5 versicolor 20.48 20.48 20.48## 选择几列> filter(test, Sepal.Width == 3.2)Sepal.Length Sepal.Width Petal.Length Petal.Width Species new happy new21 7.0 3.2 4.7 1.4 versicolor 22.40 22.40 22.402 6.4 3.2 4.5 1.5 versicolor 20.48 20.48 20.48
%>%
管道符号 %>% 可以将一个函数的输出传递给另一个函数作为参数,从而可以链接一系列分析步骤。
## 菜鸟版本> x1 = filter(iris, Sepal.Length > 3)> x2 = select(x1, c("Sepal.Length","Sepal.Width"))> x3 = arrange(x2, Sepal.Length)> head(x3)Sepal.Length Sepal.Width1 4.3 3.02 4.4 2.93 4.4 3.04 4.4 3.25 4.5 2.36 4.6 3.1## 进阶版本> x = iris %>%+ filter(Sepal.Length > 3) %>%+ select(c("Sepal.Length","Sepal.Width")) %>%+ arrange(Sepal.Length)> head(x)Sepal.Length Sepal.Width1 4.3 3.02 4.4 2.93 4.4 3.04 4.4 3.25 4.5 2.36 4.6 3.1
