如何在使用 transmute_if / transmute_at 中实现除以列的大标题
How to achieve a large tibble divided by a column within using transmute_if / transmute_at
我有一个小问题,说:
> library(tibble)
> as_tibble(iris)
# A tibble: 150 x 5
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
<dbl> <dbl> <dbl> <dbl> <fctr>
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
7 4.6 3.4 1.4 0.3 setosa
8 5.0 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
# ... with 140 more rows
我可以使用 transmute_if 或 transmute_at 将每一列划分为 Petal.width
吗?
类似于
> iris[,-c(5)]/iris[,4]
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 25.500000 17.500000 7.000000 1
2 24.500000 15.000000 7.000000 1
3 23.500000 16.000000 6.500000 1
4 23.000000 15.500000 7.500000 1
5 25.000000 18.000000 7.000000 1
6 13.500000 9.750000 4.250000 1
7 15.333333 11.333333 4.666667 1
8 25.000000 17.000000 7.500000 1
9 22.000000 14.500000 7.000000 1
10 49.000000 31.000000 15.000000 1
11 27.000000 18.500000 7.500000 1
但不知道确切的变量名,只知道.Length
和.Width
的结尾
谢谢,
我们可以使用其中任何一个。不同之处在于,假设我们只想对 numeric
列进行转换,那么 transmute_if
通过避免索引或列名
会更合适
iris %>%
as_tibble %>%
transmute_if(is.numeric, funs(./iris$Petal.Width))
如果我们已经知道列名或索引,可以使用transmute_at
iris %>%
as_tibble %>%
transmute_at(1:4, funs(./iris$Petal.Width))
如果它特定于特定的一组列,则说那些在列名称中具有“宽度”作为后缀部分的列
iris %>%
as_tibble %>%
transmute_at(vars(ends_with("Width")), funs(./iris$Petal.Width))
如果打算同时保留其他列,请使用 mutate_at
iris %>%
as_tibble %>%
mutate_at(vars(ends_with("Width")), funs(./iris$Petal.Width))
我有一个小问题,说:
> library(tibble)
> as_tibble(iris)
# A tibble: 150 x 5
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
<dbl> <dbl> <dbl> <dbl> <fctr>
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
7 4.6 3.4 1.4 0.3 setosa
8 5.0 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
# ... with 140 more rows
我可以使用 transmute_if 或 transmute_at 将每一列划分为 Petal.width
吗?
类似于
> iris[,-c(5)]/iris[,4]
Sepal.Length Sepal.Width Petal.Length Petal.Width
1 25.500000 17.500000 7.000000 1
2 24.500000 15.000000 7.000000 1
3 23.500000 16.000000 6.500000 1
4 23.000000 15.500000 7.500000 1
5 25.000000 18.000000 7.000000 1
6 13.500000 9.750000 4.250000 1
7 15.333333 11.333333 4.666667 1
8 25.000000 17.000000 7.500000 1
9 22.000000 14.500000 7.000000 1
10 49.000000 31.000000 15.000000 1
11 27.000000 18.500000 7.500000 1
但不知道确切的变量名,只知道.Length
和.Width
谢谢,
我们可以使用其中任何一个。不同之处在于,假设我们只想对 numeric
列进行转换,那么 transmute_if
通过避免索引或列名
iris %>%
as_tibble %>%
transmute_if(is.numeric, funs(./iris$Petal.Width))
如果我们已经知道列名或索引,可以使用transmute_at
iris %>%
as_tibble %>%
transmute_at(1:4, funs(./iris$Petal.Width))
如果它特定于特定的一组列,则说那些在列名称中具有“宽度”作为后缀部分的列
iris %>%
as_tibble %>%
transmute_at(vars(ends_with("Width")), funs(./iris$Petal.Width))
如果打算同时保留其他列,请使用 mutate_at
iris %>%
as_tibble %>%
mutate_at(vars(ends_with("Width")), funs(./iris$Petal.Width))