11  dplyr包,数据处理

11.1 简介

dplyr是tidyverse集合包中一个功能强大的R包,用于进行数据处理和数据操作。它提供了一组语法简洁而一致的函数,使数据的筛选、切片、排序、汇总等操作变得简单和高效。

下面是一些特别常用的dplyr函数及其用法:

11.2 select(): 选择指定的列。

library(tidyverse)

tibble(iris)
# A tibble: 150 x 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
 1          5.1         3.5          1.4         0.2 setosa 
 2          4.9         3            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           3.6          1.4         0.2 setosa 
 6          5.4         3.9          1.7         0.4 setosa 
 7          4.6         3.4          1.4         0.3 setosa 
 8          5           3.4          1.5         0.2 setosa 
 9          4.4         2.9          1.4         0.2 setosa 
10          4.9         3.1          1.5         0.1 setosa 
# i 140 more rows

11.2.1 按照索引选择列

# 选择数据的第1和第2列
tibble(iris) %>%
  select(1,2)
# A tibble: 150 x 2
   Sepal.Length Sepal.Width
          <dbl>       <dbl>
 1          5.1         3.5
 2          4.9         3  
 3          4.7         3.2
 4          4.6         3.1
 5          5           3.6
 6          5.4         3.9
 7          4.6         3.4
 8          5           3.4
 9          4.4         2.9
10          4.9         3.1
# i 140 more rows

11.2.2 按照列名选择列

# 选择数据中名为"Sepal.Length", "Sepal.Width"的两列
tibble(iris) %>%
  select(c("Sepal.Length", "Sepal.Width"))
# A tibble: 150 x 2
   Sepal.Length Sepal.Width
          <dbl>       <dbl>
 1          5.1         3.5
 2          4.9         3  
 3          4.7         3.2
 4          4.6         3.1
 5          5           3.6
 6          5.4         3.9
 7          4.6         3.4
 8          5           3.4
 9          4.4         2.9
10          4.9         3.1
# i 140 more rows

配合any_of使用

# 选择数据中名为"a","Sepal.Length", "Sepal.Width"的3列
# 配合any_of使用

tibble(iris) %>%
  select(any_of(c("a","Sepal.Length", "Sepal.Width")))
# A tibble: 150 x 2
   Sepal.Length Sepal.Width
          <dbl>       <dbl>
 1          5.1         3.5
 2          4.9         3  
 3          4.7         3.2
 4          4.6         3.1
 5          5           3.6
 6          5.4         3.9
 7          4.6         3.4
 8          5           3.4
 9          4.4         2.9
10          4.9         3.1
# i 140 more rows

any_of 在选择列时,如果某些列名不存在,它不会抛出错误,而是仅选择存在的列。

11.2.3 删除列

使用负号排除特定列

# 删除数据的第1和第2列
tibble(iris) %>%
  select(-c(1,2))
# A tibble: 150 x 3
   Petal.Length Petal.Width Species
          <dbl>       <dbl> <fct>  
 1          1.4         0.2 setosa 
 2          1.4         0.2 setosa 
 3          1.3         0.2 setosa 
 4          1.5         0.2 setosa 
 5          1.4         0.2 setosa 
 6          1.7         0.4 setosa 
 7          1.4         0.3 setosa 
 8          1.5         0.2 setosa 
 9          1.4         0.2 setosa 
10          1.5         0.1 setosa 
# i 140 more rows

11.2.4 配合函数使用,选择列

everything():选择全部列

# 选择全部的列
tibble(iris) %>%
  select(everything())
# A tibble: 150 x 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
 1          5.1         3.5          1.4         0.2 setosa 
 2          4.9         3            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           3.6          1.4         0.2 setosa 
 6          5.4         3.9          1.7         0.4 setosa 
 7          4.6         3.4          1.4         0.3 setosa 
 8          5           3.4          1.5         0.2 setosa 
 9          4.4         2.9          1.4         0.2 setosa 
10          4.9         3.1          1.5         0.1 setosa 
# i 140 more rows

last_col():选择最后一列

# 选择最后一列
tibble(iris) %>%
  select(last_col())
# A tibble: 150 x 1
   Species
   <fct>  
 1 setosa 
 2 setosa 
 3 setosa 
 4 setosa 
 5 setosa 
 6 setosa 
 7 setosa 
 8 setosa 
 9 setosa 
10 setosa 
# i 140 more rows

contains():选择包含某个字符串的列

# 选择包含Width的列
tibble(iris) %>%
  select(contains("Width"))
# A tibble: 150 x 2
   Sepal.Width Petal.Width
         <dbl>       <dbl>
 1         3.5         0.2
 2         3           0.2
 3         3.2         0.2
 4         3.1         0.2
 5         3.6         0.2
 6         3.9         0.4
 7         3.4         0.3
 8         3.4         0.2
 9         2.9         0.2
10         3.1         0.1
# i 140 more rows

starts_with():选择开头是某个字符串的列

# 选择字符开始是"Sepal"的列
tibble(iris) %>%
  select(starts_with("Sepal"))
# A tibble: 150 x 2
   Sepal.Length Sepal.Width
          <dbl>       <dbl>
 1          5.1         3.5
 2          4.9         3  
 3          4.7         3.2
 4          4.6         3.1
 5          5           3.6
 6          5.4         3.9
 7          4.6         3.4
 8          5           3.4
 9          4.4         2.9
10          4.9         3.1
# i 140 more rows

ends_with():选择结尾是某个字符串的列

# 选择字符结束是"Width"的列
tibble(iris) %>%
  select(ends_with("Width"))
# A tibble: 150 x 2
   Sepal.Width Petal.Width
         <dbl>       <dbl>
 1         3.5         0.2
 2         3           0.2
 3         3.2         0.2
 4         3.1         0.2
 5         3.6         0.2
 6         3.9         0.4
 7         3.4         0.3
 8         3.4         0.2
 9         2.9         0.2
10         3.1         0.1
# i 140 more rows

matches():通过正则表达式选择列

# 选择字符开始是"S"的列
tibble(iris) %>%
  select(matches("^S"))
# A tibble: 150 x 3
   Sepal.Length Sepal.Width Species
          <dbl>       <dbl> <fct>  
 1          5.1         3.5 setosa 
 2          4.9         3   setosa 
 3          4.7         3.2 setosa 
 4          4.6         3.1 setosa 
 5          5           3.6 setosa 
 6          5.4         3.9 setosa 
 7          4.6         3.4 setosa 
 8          5           3.4 setosa 
 9          4.4         2.9 setosa 
10          4.9         3.1 setosa 
# i 140 more rows

where(is.numeric) :根据列的数据类型选择列

# 选择数据中的数值类型的列
tibble(iris) %>%
  select(where(is.numeric))
# A tibble: 150 x 4
   Sepal.Length Sepal.Width Petal.Length Petal.Width
          <dbl>       <dbl>        <dbl>       <dbl>
 1          5.1         3.5          1.4         0.2
 2          4.9         3            1.4         0.2
 3          4.7         3.2          1.3         0.2
 4          4.6         3.1          1.5         0.2
 5          5           3.6          1.4         0.2
 6          5.4         3.9          1.7         0.4
 7          4.6         3.4          1.4         0.3
 8          5           3.4          1.5         0.2
 9          4.4         2.9          1.4         0.2
10          4.9         3.1          1.5         0.1
# i 140 more rows

11.2.5 排序

# 将名为"Species"的列排到第一位
tibble(iris) %>%
  select("Species",everything())
# A tibble: 150 x 5
   Species Sepal.Length Sepal.Width Petal.Length Petal.Width
   <fct>          <dbl>       <dbl>        <dbl>       <dbl>
 1 setosa           5.1         3.5          1.4         0.2
 2 setosa           4.9         3            1.4         0.2
 3 setosa           4.7         3.2          1.3         0.2
 4 setosa           4.6         3.1          1.5         0.2
 5 setosa           5           3.6          1.4         0.2
 6 setosa           5.4         3.9          1.7         0.4
 7 setosa           4.6         3.4          1.4         0.3
 8 setosa           5           3.4          1.5         0.2
 9 setosa           4.4         2.9          1.4         0.2
10 setosa           4.9         3.1          1.5         0.1
# i 140 more rows