tidyverse是R的一个包,可以让数据处理变得更加简洁。其中自带八个常用的包ggplot2/ tibble/ tidyr/ readr/ purrr/ dplyr/ stringr/forcat,可以实现常见的数据处理和可视化操作。

数据框排序

sort

base包的函数。给向量排序,返回从小到大的排序,参数是decreasing,可以是T或F

  1. > test
  2. Sepal.Length Sepal.Width Petal.Length Petal.Width Species
  3. 1 5.1 3.5 1.4 0.2 setosa
  4. 2 4.9 3.0 1.4 0.2 setosa
  5. 3 7.0 3.2 4.7 1.4 versicolor
  6. 4 6.4 3.2 4.5 1.5 versicolor
  7. 5 6.3 3.3 6.0 2.5 virginica
  8. 6 5.8 2.7 5.1 1.9 virginica
  9. ## 向量排序(升序)
  10. > sort(test$Sepal.Length)
  11. [1] 4.9 5.1 5.8 6.3 6.4 7.0
  12. ## 向量排序(降序)
  13. > sort(test$Sepal.Length,decreasing = T)
  14. [1] 7.0 6.4 6.3 5.8 5.1 4.9

order

base包的函数。可以给向量或者数据框排序,返回sort排序的下标,利用此逻辑给数据框排序。

  1. > test
  2. Sepal.Length Sepal.Width Petal.Length Petal.Width Species
  3. 1 5.1 3.5 1.4 0.2 setosa
  4. 2 4.9 3.0 1.4 0.2 setosa
  5. 3 7.0 3.2 4.7 1.4 versicolor
  6. 4 6.4 3.2 4.5 1.5 versicolor
  7. 5 6.3 3.3 6.0 2.5 virginica
  8. 6 5.8 2.7 5.1 1.9 virginica
  9. ## 给数据框排序(正序)
  10. > test[order(test$Sepal.Length),]
  11. Sepal.Length Sepal.Width Petal.Length Petal.Width Species
  12. 2 4.9 3.0 1.4 0.2 setosa
  13. 1 5.1 3.5 1.4 0.2 setosa
  14. 6 5.8 2.7 5.1 1.9 virginica
  15. 5 6.3 3.3 6.0 2.5 virginica
  16. 4 6.4 3.2 4.5 1.5 versicolor
  17. 3 7.0 3.2 4.7 1.4 versicolor
  18. ## 给数据框排序(降序)
  19. > test[order(test$Sepal.Length,decreasing = T),]
  20. Sepal.Length Sepal.Width Petal.Length Petal.Width Species
  21. 3 7.0 3.2 4.7 1.4 versicolor
  22. 4 6.4 3.2 4.5 1.5 versicolor
  23. 5 6.3 3.3 6.0 2.5 virginica
  24. 6 5.8 2.7 5.1 1.9 virginica
  25. 1 5.1 3.5 1.4 0.2 setosa
  26. 2 4.9 3.0 1.4 0.2 setosa

arrange

tidyverse包的函数,可以实现简单的代码排序,多个条件排序等操作。

  1. > test
  2. Sepal.Length Sepal.Width Petal.Length Petal.Width Species
  3. 1 5.1 3.5 1.4 0.2 setosa
  4. 2 4.9 3.0 1.4 0.2 setosa
  5. 3 7.0 3.2 4.7 1.4 versicolor
  6. 4 6.4 3.2 4.5 1.5 versicolor
  7. 5 6.3 3.3 6.0 2.5 virginica
  8. 6 5.8 2.7 5.1 1.9 virginica
  9. ## 数据框排序(正序)
  10. > library(dplyr)
  11. > arrange(test, Sepal.Length)
  12. Sepal.Length Sepal.Width Petal.Length Petal.Width Species
  13. 1 4.9 3.0 1.4 0.2 setosa
  14. 2 5.1 3.5 1.4 0.2 setosa
  15. 3 5.8 2.7 5.1 1.9 virginica
  16. 4 6.3 3.3 6.0 2.5 virginica
  17. 5 6.4 3.2 4.5 1.5 versicolor
  18. 6 7.0 3.2 4.7 1.4 versicolor
  19. ## 数据框排序(降序)
  20. > arrange(test, desc(Sepal.Length))
  21. Sepal.Length Sepal.Width Petal.Length Petal.Width Species
  22. 1 7.0 3.2 4.7 1.4 versicolor
  23. 2 6.4 3.2 4.5 1.5 versicolor
  24. 3 6.3 3.3 6.0 2.5 virginica
  25. 4 5.8 2.7 5.1 1.9 virginica
  26. 5 5.1 3.5 1.4 0.2 setosa
  27. 6 4.9 3.0 1.4 0.2 setosa
  28. ## 多条件排序(先Sepal.Width降序,再Sepal.Length升序)
  29. > arrange(test, desc(Sepal.Width),Sepal.Length)
  30. Sepal.Length Sepal.Width Petal.Length Petal.Width Species
  31. 1 5.1 3.5 1.4 0.2 setosa
  32. 2 6.3 3.3 6.0 2.5 virginica
  33. 3 6.4 3.2 4.5 1.5 versicolor
  34. 4 7.0 3.2 4.7 1.4 versicolor
  35. 5 4.9 3.0 1.4 0.2 setosa
  36. 6 5.8 2.7 5.1 1.9 virginica

来自tidyr的几个函数

mutate

增加一列
test = mutate(test, new = Sepal.Length Sepal.Width)
*new
代表新列名。

  1. > test
  2. Sepal.Length Sepal.Width Petal.Length Petal.Width Species
  3. 1 5.1 3.5 1.4 0.2 setosa
  4. 2 4.9 3.0 1.4 0.2 setosa
  5. 3 7.0 3.2 4.7 1.4 versicolor
  6. 4 6.4 3.2 4.5 1.5 versicolor
  7. 5 6.3 3.3 6.0 2.5 virginica
  8. 6 5.8 2.7 5.1 1.9 virginica
  9. > test = mutate(test, new = Sepal.Length * Sepal.Width)
  10. > test
  11. Sepal.Length Sepal.Width Petal.Length Petal.Width Species new
  12. 1 5.1 3.5 1.4 0.2 setosa 17.85
  13. 2 4.9 3.0 1.4 0.2 setosa 14.70
  14. 3 7.0 3.2 4.7 1.4 versicolor 22.40
  15. 4 6.4 3.2 4.5 1.5 versicolor 20.48
  16. 5 6.3 3.3 6.0 2.5 virginica 20.79
  17. 6 5.8 2.7 5.1 1.9 virginica 15.66
  18. ## 简化代码
  19. > test
  20. Sepal.Length Sepal.Width Petal.Length Petal.Width Species new happy
  21. 1 5.1 3.5 1.4 0.2 setosa 17.85 17.85
  22. 2 4.9 3.0 1.4 0.2 setosa 14.70 14.70
  23. 3 7.0 3.2 4.7 1.4 versicolor 22.40 22.40
  24. 4 6.4 3.2 4.5 1.5 versicolor 20.48 20.48
  25. 5 6.3 3.3 6.0 2.5 virginica 20.79 20.79
  26. 6 5.8 2.7 5.1 1.9 virginica 15.66 15.66
  27. > test$new2 = test$Sepal.Length * test$Sepal.Width
  28. > test
  29. Sepal.Length Sepal.Width Petal.Length Petal.Width Species new happy new2
  30. 1 5.1 3.5 1.4 0.2 setosa 17.85 17.85 17.85
  31. 2 4.9 3.0 1.4 0.2 setosa 14.70 14.70 14.70
  32. 3 7.0 3.2 4.7 1.4 versicolor 22.40 22.40 22.40
  33. 4 6.4 3.2 4.5 1.5 versicolor 20.48 20.48 20.48
  34. 5 6.3 3.3 6.0 2.5 virginica 20.79 20.79 20.79
  35. 6 5.8 2.7 5.1 1.9 virginica 15.66 15.66 15.66

select

选择某一列或者某几列

  1. > test
  2. Sepal.Length Sepal.Width Petal.Length Petal.Width Species new happy new2
  3. 1 5.1 3.5 1.4 0.2 setosa 17.85 17.85 17.85
  4. 2 4.9 3.0 1.4 0.2 setosa 14.70 14.70 14.70
  5. 3 7.0 3.2 4.7 1.4 versicolor 22.40 22.40 22.40
  6. 4 6.4 3.2 4.5 1.5 versicolor 20.48 20.48 20.48
  7. 5 6.3 3.3 6.0 2.5 virginica 20.79 20.79 20.79
  8. 6 5.8 2.7 5.1 1.9 virginica 15.66 15.66 15.66
  9. ## 选择某一列
  10. > select(test, Sepal.Length)
  11. Sepal.Length
  12. 1 5.1
  13. 2 4.9
  14. 3 7.0
  15. 4 6.4
  16. 5 6.3
  17. 6 5.8
  18. ## 选择某几列
  19. > select(test, starts_with("n"))
  20. new new2
  21. 1 17.85 17.85
  22. 2 14.70 14.70
  23. 3 22.40 22.40
  24. 4 20.48 20.48
  25. 5 20.79 20.79
  26. 6 15.66 15.66

filter

选择某一行或者某几行

  1. > test
  2. Sepal.Length Sepal.Width Petal.Length Petal.Width Species new happy new2
  3. 1 5.1 3.5 1.4 0.2 setosa 17.85 17.85 17.85
  4. 2 4.9 3.0 1.4 0.2 setosa 14.70 14.70 14.70
  5. 3 7.0 3.2 4.7 1.4 versicolor 22.40 22.40 22.40
  6. 4 6.4 3.2 4.5 1.5 versicolor 20.48 20.48 20.48
  7. 5 6.3 3.3 6.0 2.5 virginica 20.79 20.79 20.79
  8. 6 5.8 2.7 5.1 1.9 virginica 15.66 15.66 15.66
  9. ## 选择一列
  10. > filter(test, Sepal.Length == 6.4)
  11. Sepal.Length Sepal.Width Petal.Length Petal.Width Species new happy new2
  12. 1 6.4 3.2 4.5 1.5 versicolor 20.48 20.48 20.48
  13. ## 选择几列
  14. > filter(test, Sepal.Width == 3.2)
  15. Sepal.Length Sepal.Width Petal.Length Petal.Width Species new happy new2
  16. 1 7.0 3.2 4.7 1.4 versicolor 22.40 22.40 22.40
  17. 2 6.4 3.2 4.5 1.5 versicolor 20.48 20.48 20.48

%>%

管道符号 %>% 可以将一个函数的输出传递给另一个函数作为参数,从而可以链接一系列分析步骤。

  1. ## 菜鸟版本
  2. > x1 = filter(iris, Sepal.Length > 3)
  3. > x2 = select(x1, c("Sepal.Length","Sepal.Width"))
  4. > x3 = arrange(x2, Sepal.Length)
  5. > head(x3)
  6. Sepal.Length Sepal.Width
  7. 1 4.3 3.0
  8. 2 4.4 2.9
  9. 3 4.4 3.0
  10. 4 4.4 3.2
  11. 5 4.5 2.3
  12. 6 4.6 3.1
  13. ## 进阶版本
  14. > x = iris %>%
  15. + filter(Sepal.Length > 3) %>%
  16. + select(c("Sepal.Length","Sepal.Width")) %>%
  17. + arrange(Sepal.Length)
  18. > head(x)
  19. Sepal.Length Sepal.Width
  20. 1 4.3 3.0
  21. 2 4.4 2.9
  22. 3 4.4 3.0
  23. 4 4.4 3.2
  24. 5 4.5 2.3
  25. 6 4.6 3.1