tidyverse是R的一个包,可以让数据处理变得更加简洁。其中自带八个常用的包ggplot2/ tibble/ tidyr/ readr/ purrr/ dplyr/ stringr/forcat,可以实现常见的数据处理和可视化操作。
数据框排序
sort
base包的函数。给向量排序,返回从小到大的排序,参数是decreasing,可以是T或F。
> test
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 7.0 3.2 4.7 1.4 versicolor
4 6.4 3.2 4.5 1.5 versicolor
5 6.3 3.3 6.0 2.5 virginica
6 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排序的下标,利用此逻辑给数据框排序。
> test
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 7.0 3.2 4.7 1.4 versicolor
4 6.4 3.2 4.5 1.5 versicolor
5 6.3 3.3 6.0 2.5 virginica
6 5.8 2.7 5.1 1.9 virginica
## 给数据框排序(正序)
> test[order(test$Sepal.Length),]
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
2 4.9 3.0 1.4 0.2 setosa
1 5.1 3.5 1.4 0.2 setosa
6 5.8 2.7 5.1 1.9 virginica
5 6.3 3.3 6.0 2.5 virginica
4 6.4 3.2 4.5 1.5 versicolor
3 7.0 3.2 4.7 1.4 versicolor
## 给数据框排序(降序)
> test[order(test$Sepal.Length,decreasing = T),]
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
3 7.0 3.2 4.7 1.4 versicolor
4 6.4 3.2 4.5 1.5 versicolor
5 6.3 3.3 6.0 2.5 virginica
6 5.8 2.7 5.1 1.9 virginica
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
arrange
是tidyverse包的函数,可以实现简单的代码排序,多个条件排序等操作。
> test
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 7.0 3.2 4.7 1.4 versicolor
4 6.4 3.2 4.5 1.5 versicolor
5 6.3 3.3 6.0 2.5 virginica
6 5.8 2.7 5.1 1.9 virginica
## 数据框排序(正序)
> library(dplyr)
> arrange(test, Sepal.Length)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 4.9 3.0 1.4 0.2 setosa
2 5.1 3.5 1.4 0.2 setosa
3 5.8 2.7 5.1 1.9 virginica
4 6.3 3.3 6.0 2.5 virginica
5 6.4 3.2 4.5 1.5 versicolor
6 7.0 3.2 4.7 1.4 versicolor
## 数据框排序(降序)
> arrange(test, desc(Sepal.Length))
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 7.0 3.2 4.7 1.4 versicolor
2 6.4 3.2 4.5 1.5 versicolor
3 6.3 3.3 6.0 2.5 virginica
4 5.8 2.7 5.1 1.9 virginica
5 5.1 3.5 1.4 0.2 setosa
6 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 Species
1 5.1 3.5 1.4 0.2 setosa
2 6.3 3.3 6.0 2.5 virginica
3 6.4 3.2 4.5 1.5 versicolor
4 7.0 3.2 4.7 1.4 versicolor
5 4.9 3.0 1.4 0.2 setosa
6 5.8 2.7 5.1 1.9 virginica
来自tidyr的几个函数
mutate
增加一列
test = mutate(test, new = Sepal.Length Sepal.Width)
*new代表新列名。
> test
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 7.0 3.2 4.7 1.4 versicolor
4 6.4 3.2 4.5 1.5 versicolor
5 6.3 3.3 6.0 2.5 virginica
6 5.8 2.7 5.1 1.9 virginica
> test = mutate(test, new = Sepal.Length * Sepal.Width)
> test
Sepal.Length Sepal.Width Petal.Length Petal.Width Species new
1 5.1 3.5 1.4 0.2 setosa 17.85
2 4.9 3.0 1.4 0.2 setosa 14.70
3 7.0 3.2 4.7 1.4 versicolor 22.40
4 6.4 3.2 4.5 1.5 versicolor 20.48
5 6.3 3.3 6.0 2.5 virginica 20.79
6 5.8 2.7 5.1 1.9 virginica 15.66
## 简化代码
> test
Sepal.Length Sepal.Width Petal.Length Petal.Width Species new happy
1 5.1 3.5 1.4 0.2 setosa 17.85 17.85
2 4.9 3.0 1.4 0.2 setosa 14.70 14.70
3 7.0 3.2 4.7 1.4 versicolor 22.40 22.40
4 6.4 3.2 4.5 1.5 versicolor 20.48 20.48
5 6.3 3.3 6.0 2.5 virginica 20.79 20.79
6 5.8 2.7 5.1 1.9 virginica 15.66 15.66
> test$new2 = test$Sepal.Length * test$Sepal.Width
> test
Sepal.Length Sepal.Width Petal.Length Petal.Width Species new happy new2
1 5.1 3.5 1.4 0.2 setosa 17.85 17.85 17.85
2 4.9 3.0 1.4 0.2 setosa 14.70 14.70 14.70
3 7.0 3.2 4.7 1.4 versicolor 22.40 22.40 22.40
4 6.4 3.2 4.5 1.5 versicolor 20.48 20.48 20.48
5 6.3 3.3 6.0 2.5 virginica 20.79 20.79 20.79
6 5.8 2.7 5.1 1.9 virginica 15.66 15.66 15.66
select
选择某一列或者某几列。
> test
Sepal.Length Sepal.Width Petal.Length Petal.Width Species new happy new2
1 5.1 3.5 1.4 0.2 setosa 17.85 17.85 17.85
2 4.9 3.0 1.4 0.2 setosa 14.70 14.70 14.70
3 7.0 3.2 4.7 1.4 versicolor 22.40 22.40 22.40
4 6.4 3.2 4.5 1.5 versicolor 20.48 20.48 20.48
5 6.3 3.3 6.0 2.5 virginica 20.79 20.79 20.79
6 5.8 2.7 5.1 1.9 virginica 15.66 15.66 15.66
## 选择某一列
> select(test, Sepal.Length)
Sepal.Length
1 5.1
2 4.9
3 7.0
4 6.4
5 6.3
6 5.8
## 选择某几列
> select(test, starts_with("n"))
new new2
1 17.85 17.85
2 14.70 14.70
3 22.40 22.40
4 20.48 20.48
5 20.79 20.79
6 15.66 15.66
filter
选择某一行或者某几行。
> test
Sepal.Length Sepal.Width Petal.Length Petal.Width Species new happy new2
1 5.1 3.5 1.4 0.2 setosa 17.85 17.85 17.85
2 4.9 3.0 1.4 0.2 setosa 14.70 14.70 14.70
3 7.0 3.2 4.7 1.4 versicolor 22.40 22.40 22.40
4 6.4 3.2 4.5 1.5 versicolor 20.48 20.48 20.48
5 6.3 3.3 6.0 2.5 virginica 20.79 20.79 20.79
6 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 new2
1 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 new2
1 7.0 3.2 4.7 1.4 versicolor 22.40 22.40 22.40
2 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.Width
1 4.3 3.0
2 4.4 2.9
3 4.4 3.0
4 4.4 3.2
5 4.5 2.3
6 4.6 3.1
## 进阶版本
> x = iris %>%
+ filter(Sepal.Length > 3) %>%
+ select(c("Sepal.Length","Sepal.Width")) %>%
+ arrange(Sepal.Length)
> head(x)
Sepal.Length Sepal.Width
1 4.3 3.0
2 4.4 2.9
3 4.4 3.0
4 4.4 3.2
5 4.5 2.3
6 4.6 3.1