# 1 dplyr包

### 1.1 展现数据 tbl_df()：

> mtcars_df <- tbl_df(mtcars)
> mtcars_df
# A tibble: 32 × 11
mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
*  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1   21.0     6 160.0   110  3.90 2.620 16.46     0     1     4     4
2   21.0     6 160.0   110  3.90 2.875 17.02     0     1     4     4
3   22.8     4 108.0    93  3.85 2.320 18.61     1     1     4     1
4   21.4     6 258.0   110  3.08 3.215 19.44     1     0     3     1
5   18.7     8 360.0   175  3.15 3.440 17.02     0     0     3     2
6   18.1     6 225.0   105  2.76 3.460 20.22     1     0     3     1
7   14.3     8 360.0   245  3.21 3.570 15.84     0     0     3     4
8   24.4     4 146.7    62  3.69 3.190 20.00     1     0     4     2
9   22.8     4 140.8    95  3.92 3.150 22.90     1     0     4     2
10  19.2     6 167.6   123  3.92 3.440 18.30     1     0     4     4
# ... with 22 more rows

### 1.2 筛选数据 filter()：

> filter(mtcars_df, mpg == 21, cyl == 6)
# A tibble: 2 × 11
mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1    21     6   160   110   3.9 2.620 16.46     0     1     4     4
2    21     6   160   110   3.9 2.875 17.02     0     1     4     4

### 1.3 排列数据 arrange()：

# 先对cyl列正序排列，再对carb列倒序排列
> print(arrange(mtcars_df, cyl, desc(carb)))
mpg cyl  disp  hp drat    wt  qsec vs am gear carb
1  24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2
2  22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2
3  30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2
4  26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
5  30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
6  21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2
7  22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
8  32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1
9  33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1
10 21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1
11 27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1
12 19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
13 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
14 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
15 19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4
16 17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4
17 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
18 18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
19 15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
20 14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4
21 10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4
22 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4
23 14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4
24 13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4
25 15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
26 16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3
27 17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3
28 15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3
29 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
30 15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2
31 15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2
32 19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2

### 1.4 选择子集 select()：

> select(mtcars_df, mpg, disp:wt, carb) # disp:wt表示从disp列选到wt列
# A tibble: 32 × 6
mpg  disp    hp  drat    wt  carb
*  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1   21.0 160.0   110  3.90 2.620     4
2   21.0 160.0   110  3.90 2.875     4
3   22.8 108.0    93  3.85 2.320     1
4   21.4 258.0   110  3.08 3.215     1
5   18.7 360.0   175  3.15 3.440     2
6   18.1 225.0   105  2.76 3.460     1
7   14.3 360.0   245  3.21 3.570     4
8   24.4 146.7    62  3.69 3.190     2
9   22.8 140.8    95  3.92 3.150     2
10  19.2 167.6   123  3.92 3.440     4
# ... with 22 more rows

### 1.5 变形 mutate()：

> mutate(mtcars_df, NO = 1:dim(mtcars_df)[1], res = hp - drat)
# A tibble: 32 × 13
mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb    NO    res
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int>  <dbl>
1   21.0     6 160.0   110  3.90 2.620 16.46     0     1     4     4     1 106.10
2   21.0     6 160.0   110  3.90 2.875 17.02     0     1     4     4     2 106.10
3   22.8     4 108.0    93  3.85 2.320 18.61     1     1     4     1     3  89.15
4   21.4     6 258.0   110  3.08 3.215 19.44     1     0     3     1     4 106.92
5   18.7     8 360.0   175  3.15 3.440 17.02     0     0     3     2     5 171.85
6   18.1     6 225.0   105  2.76 3.460 20.22     1     0     3     1     6 102.24
7   14.3     8 360.0   245  3.21 3.570 15.84     0     0     3     4     7 241.79
8   24.4     4 146.7    62  3.69 3.190 20.00     1     0     4     2     8  58.31
9   22.8     4 140.8    95  3.92 3.150 22.90     1     0     4     2     9  91.08
10  19.2     6 167.6   123  3.92 3.440 18.30     1     0     4     4    10 119.08
# ... with 22 more rows

### 1.6 总览数据 summarise()：

> summarise(mtcars_df, mdisp = mean(disp, na.rm = TRUE), mhp = mean(hp, na.rm = TRUE))
# A tibble: 1 × 2
mdisp      mhp
<dbl>    <dbl>
1 230.7219 146.6875

### 1.7 数据分组 group_by()：

> cars <- group_by(mtcars_df, cyl)
> (countcars <- summarise(cars, count = n())) # count = n()用来计算次数
# A tibble: 3 × 2
cyl count
<dbl> <int>
1     4    11
2     6     7
3     8    14

# 2 tidyr包

### 2.1 宽转长 gather()：

# gather(data, key, value, …, na.rm = FALSE, convert = FALSE)
# data：需要被转换的宽形表
# key：将原数据框中的所有列赋给一个新变量key
# value：将原数据框中的所有值赋给一个新变量value
# …：可以指定哪些列聚到同一列中
# na.rm：是否删除缺失值

> (longdata <- gather(mtcars_df, attribute, value, -mpg))
# A tibble: 320 × 3
mpg attribute value
<dbl>     <chr> <dbl>
1   21.0       cyl     6
2   21.0       cyl     6
3   22.8       cyl     4
4   21.4       cyl     6
5   18.7       cyl     8
6   18.1       cyl     6
7   14.3       cyl     8
8   24.4       cyl     4
9   22.8       cyl     4
10  19.2       cyl     6
# ... with 310 more rows
> (longdata <- gather(mtcars_df, attribute, value))
# A tibble: 352 × 2
attribute value
<chr> <dbl>
1        mpg  21.0
2        mpg  21.0
3        mpg  22.8
4        mpg  21.4
5        mpg  18.7
6        mpg  18.1
7        mpg  14.3
8        mpg  24.4
9        mpg  22.8
10       mpg  19.2
# ... with 342 more rows
> (longdata <- gather(mtcars_df, attribute, value, mpg, carb))
# A tibble: 64 × 11
cyl  disp    hp  drat    wt  qsec    vs    am  gear attribute value
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>     <chr> <dbl>
1      6 160.0   110  3.90 2.620 16.46     0     1     4       mpg  21.0
2      6 160.0   110  3.90 2.875 17.02     0     1     4       mpg  21.0
3      4 108.0    93  3.85 2.320 18.61     1     1     4       mpg  22.8
4      6 258.0   110  3.08 3.215 19.44     1     0     3       mpg  21.4
5      8 360.0   175  3.15 3.440 17.02     0     0     3       mpg  18.7
6      6 225.0   105  2.76 3.460 20.22     1     0     3       mpg  18.1
7      8 360.0   245  3.21 3.570 15.84     0     0     3       mpg  14.3
8      4 146.7    62  3.69 3.190 20.00     1     0     4       mpg  24.4
9      4 140.8    95  3.92 3.150 22.90     1     0     4       mpg  22.8
10     6 167.6   123  3.92 3.440 18.30     1     0     4       mpg  19.2
# ... with 54 more rows

# spread(data, key, value, fill = NA, convert = FALSE, drop = TRUE)
# data：为需要转换的长形表
# key：需要将变量值拓展为字段的变量
# value：需要分散的值
# fill：对于缺失值，可将fill的值赋值给被转型后的缺失值

# A tibble: 32 × 11
cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb   mpg
*  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1      4  71.1    65  4.22 1.835 19.90     1     1     4     1  33.9
2      4  75.7    52  4.93 1.615 18.52     1     1     4     2  30.4
3      4  78.7    66  4.08 2.200 19.47     1     1     4     1  32.4
4      4  79.0    66  4.08 1.935 18.90     1     1     4     1  27.3
5      4  95.1   113  3.77 1.513 16.90     1     1     5     2  30.4
6      4 108.0    93  3.85 2.320 18.61     1     1     4     1  22.8
7      4 120.1    97  3.70 2.465 20.01     1     0     3     1  21.5
8      4 120.3    91  4.43 2.140 16.70     0     1     5     2  26.0
9      4 121.0   109  4.11 2.780 18.60     1     1     4     2  21.4
10     4 140.8    95  3.92 3.150 22.90     1     0     4     2  22.8
# ... with 22 more rows

### 2.3 合并 unit()：

unite的调用格式如下

# unite(data, col, …, sep = “_”, remove = TRUE)
# data：为数据框
# col：被组合的新列名称
# …：指定哪些列需要被组合
# sep：组合列之间的连接符，默认为下划线
# remove：是否删除被组合的列

> TIMES <- data.frame(years = c('1990', '1991', '1992'), months = c(2,3,4), day = c(1, 2, 3))
> (TIMESunite <- unite(TIMES, information, months, day, sep= "-"))
years information
1  1990         2-1
2  1991         3-2
3  1992         4-3

### 2.4 拆分 separate()：

separate()函数可将一列拆分为多列，一般可用于日志数据或日期时间型数据的拆分，语法如下

# separate(data, col, into, sep = “[^[:alnum:]]+”, remove = TRUE,
# convert = FALSE, extra = “warn”, fill = “warn”, …)
# data：为数据框
# col：需要被拆分的列
# into：新建的列名，为字符串向量
# sep：被拆分列的分隔符
# remove：是否删除被分割的列

> (TIMESsep <- separate(TIMESunite, information, c("months", "day"), sep = "-"))
years months day
1  1990      2   1
2  1991      3   2
3  1992      4   3