管道操作
主要目的是减少不必要的中间变量x %>% f(y)其实是f(x, y)
by_species <- iris %>%
group_by(Species)summary()函数会对 列 进行处理,并且 创建新的列表 ,简单来说就是把向量作为输入值,输出单个数值。
summarise(.data, ...)> summarise(mtcars, avg = mean(mpg))
avg
1 20.09062⚠️:当summarise的时候,想要用到原来的列名,又想要继续对列处理,记得加``符号把列名括住,比如:
summarise(`Sig1` = mean(Sig1),
`Sig2` = mean(Sig2),
`Sig3` = mean(Sig3),
`Sig4` = mean(Sig4),
`Sig5` = mean(Sig5),
`Sig6` = mean(Sig6))count(x, ..., wt = NULL, sort = FALSE)按照...这个列内容是否相同的进行分组,计算同一组有多少 行。参数: x:数据 ...:根据哪列对数据进行分组 wt: sort:
> count(iris, Species)
# A tibble: 3 x 2
Species n
<fct> <int>
1 setosa 50
2 versicolor 50
3 virginica 50summarise_all(.tbl, .funs, enquo(.funs), caller_env(), ...)对所有列进行function处理summarise_at()对特定的列进行function处理summarise_if()对所有属于一个类型的列进行处理group_by(.data, ..., add = FALSE)根据...对数据进行分组后返回,不会保存成新数据,需要另外赋值参数:
add = FALSE原始数据基础上加新的分类
ungroup(x, ...)分组后,返回没有分组的数据,除去之前的分组(不是必须的操作)对行的基本操作
filter(.data, ...)筛选出符合...的数据distinct(.data, ..., .keep_all = FALSE)根据...查看不重复的数据参数:
.keep_allTRUE表示保留其他列,FALSE表示仅保留这一列
> distinct(iris, Species, .keep_all = FALSE)
Species
1 setosa
2 versicolor
3 virginica
> distinct(iris, Species, .keep_all = TRUE)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 7.0 3.2 4.7 1.4 versicolor
3 6.3 3.3 6.0 2.5 virginicaslice(.data, ...)根据具体位置选 行> slice(iris, 10:15)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 4.9 3.1 1.5 0.1 setosa
2 5.4 3.7 1.5 0.2 setosa
3 4.8 3.4 1.6 0.2 setosa
4 4.8 3.0 1.4 0.1 setosa
5 4.3 3.0 1.1 0.1 setosa
6 5.8 4.0 1.2 0.2 setosaarrange(.data, ...)根据列的内容对 行进行升序排列,加上desc()就是降序arrange(mtcars, mpg) %>% head()
arrange(mtcars, desc(mpg)) %>% head()add_row(.data, ..., .before = NULL, .after = NULL)增加一行或多行 举例:增加eruptions值是1,waiting值是1的一行add_row(faithful, eruptions = 1, waiting = 1)pull(,data, var = -1)提取数据框中某列内容出来> pull(iris, Sepal.Length)
[1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1 5.7
[20] 5.1 5.4 5.1 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8 5.4 5.2 5.5 4.9 5.0 5.5 4.9
[39] 4.4 5.1 5.0 4.5 4.4 5.0 5.1 4.8 5.1 4.6 5.3 5.0 7.0 6.4 6.9 5.5 6.5 5.7 6.3
[58] 4.9 6.6 5.2 5.0 5.9 6.0 6.1 5.6 6.7 5.6 5.8 6.2 5.6 5.9 6.1 6.3 6.1 6.4 6.6
[77] 6.8 6.7 6.0 5.7 5.5 5.5 5.8 6.0 5.4 6.0 6.7 6.3 5.6 5.5 5.5 6.1 5.8 5.0 5.6
[96] 5.7 5.7 6.2 5.1 5.7 6.3 5.8 7.1 6.3 6.5 7.6 4.9 7.3 6.7 7.2 6.5 6.4 6.8 5.7
[115] 5.8 6.4 6.5 7.7 7.7 6.0 6.9 5.6 7.7 6.3 6.7 7.2 6.2 6.1 6.4 7.2 7.4 7.9 6.4
[134] 6.3 6.1 7.7 6.3 6.4 6.0 6.9 6.7 6.9 5.8 6.8 6.7 6.7 6.3 6.5 6.2 5.9select(.data, ...)根据各种条件在数据框中提取数据 举例:> select(iris, Sepal.Length, Species)
Sepal.Length Species
1 5.1 setosa
2 4.9 setosa
3 4.7 setosamutate(.data, ...)生成新列> mutate(mtcars, gpm = 1/mpg)
mpg cyl disp hp drat wt qsec vs am gear carb gpm
1 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 0.04761905
2 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 0.04761905
3 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 0.04385965transmute(.data, ...)只显示新生成的列> transmute(mtcars, gpm = 1/mpg)
gpm
1 0.04761905
2 0.04761905
3 0.04385965mutate_all(.tbl, .funs, ...)把.funs函数应用到数据框的每列> faithful
eruptions waiting
1 3.600 79
2 1.800 54
3 3.333 74> mutate_all(faithful, funs(log(.), log2(.)))
eruptions waiting eruptions_log waiting_log eruptions_log2 waiting_log2
1 3.600 79 1.2809338 4.369448 1.8479969 6.303781
2 1.800 54 0.5877867 3.988984 0.8479969 5.754888
3 3.333 74 1.2038728 4.304065 1.7368213 6.209453mutate_if(data, 判断条件, funtion)> mutate_if(iris, is.numeric, funs(log(.)))
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 1.629241 1.2527630 0.33647224 -1.60943791 setosa
2 1.589235 1.0986123 0.33647224 -1.60943791 setosa
3 1.547563 1.1631508 0.26236426 -1.60943791 setosamutate_at(.tbl, .cols, .funs, ...)把函数应用到数据框指定的列 举例:vars(-Species)表示除了这列,对其他的进行运算> mutate_at(iris, vars(-Species), funs(log(.)))
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 1.629241 1.2527630 0.33647224 -1.60943791 setosa
2 1.589235 1.0986123 0.33647224 -1.60943791 setosa
3 1.547563 1.1631508 0.26236426 -1.60943791 setosaadd_column(.data, ..., .before = NULL, .after = NULL)增加新的一列 举例:> add_column(mtcars, new = 1:32)
mpg cyl disp hp drat wt qsec vs am gear carb new
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 1
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 2
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 3rename(.data, 新名 = 旧名)对列重命名> rename(iris, Length = Sepal.Length)
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 4.7 3.2 1.3 0.2 setosarownames_to_column()把行名变成第一列的内容
举例:
> a <- rownames_to_column(iris, var = "C")
> head(a)
C Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 1 5.1 3.5 1.4 0.2 setosa
2 2 4.9 3.0 1.4 0.2 setosa
3 3 4.7 3.2 1.3 0.2 setosa
4 4 4.6 3.1 1.5 0.2 setosa
5 5 5.0 3.6 1.4 0.2 setosa
6 6 5.4 3.9 1.7 0.4 setosacolumn_to_rownames()把第一列作为行名
举例: 注意,把列的内容转换成行名的时候,该列的内容不能够存在重复
> b <- column_to_rownames(iris, var = "Species")
Error in `.rowNamesDF<-`(x, value = value) :
duplicate 'row.names' are not allowed
In addition: Warning message:
non-unique values when setting 'row.names': ‘setosa’, ‘versicolor’, ‘virginica’
> b <- column_to_rownames(a, var = "C")
> b
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 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa根据列整合数据框
bind_cols()把两个数据框合并,两个数据框的行数要一致
> d1 <- data.frame(1:2)
> d1
X1.2
1 1
2 2
> d2 <- data.frame(1:3)
> d2
X1.3
1 1
2 2
3 3
> bind_cols(d1,d2)
Error: Argument 2 must be length 2, not 3left_join(x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ...)把数据框y中和x匹配上的进行整合
⚠️by = c("col1", "col2", "col3", ...)举例:left_join(x, y, by = "A")

⚠️:by = c("col1" = "col2")举例:left_join(x, y, by = c("C" = "D"))把y数据框中D列的内容当作索引,把y根据索引C去匹配x数据框中D中的内容,匹配上就把y中其他列的内容根据索引组合上去。

⚠️:suffix两个数据框中有相同名称的列,即使不匹配也保留相同的名称 举例:left_join(x, y, by = c("C" = "D"), suffix = c("1", "2"))

right_join(x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ...)
inner_join(x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ...)
full_join(x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ...)

bind_rows(..., id = NULL)id是增加的新列的名字intersect(x, y, ...)取得同时出现在x和y两个数据框中的行
setdiff(x, y, ...)出现在x中但是不在y中的行
union(x, y, ...)整合出现在x数据框中或y数据框中的,去除了两个数据框中重复的部分,想要保留重复的话使用union_all()
setequal()测试两个数据集是不是包含了完全相同的两行semi_join(x, y, by = NULL, ...)返回x数据集中能够匹配到y数据集中的行
anti_join(x, y, by = NULL)返回x数据集中不能够匹配到y数据集中的行