我想使用dplyr::mutate()在数据帧中创建多个新列。列名及其内容应该是动态生成的。
虹膜数据示例:
library(dplyr)
iris <- as_tibble(iris)
我创建了一个函数来改变花瓣中的新列。宽度变量:
multipetal <- function(df, n) {
varname <- paste("petal", n , sep=".")
df <- mutate(df, varname = Petal.Width * n) ## problem arises here
df
}
现在我创建了一个循环来构建我的列:
for(i in 2:5) {
iris <- multipetal(df=iris, n=i)
}
然而,由于mutate认为varname是一个字面变量名,因此循环只创建了一个新变量(称为varname),而不是四个(称为花瓣)。2 -花瓣。5)。
我怎么能得到mutate()使用我的动态名称作为变量名?
在rlang 0.4.0中,我们有了卷曲操作符({{}}),这使得这非常容易。当一个动态列名出现在赋值的左边时,使用:=。
library(dplyr)
library(rlang)
iris1 <- tbl_df(iris)
multipetal <- function(df, n) {
varname <- paste("petal", n , sep=".")
mutate(df, {{varname}} := Petal.Width * n)
}
multipetal(iris1, 4)
# A tibble: 150 x 6
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species petal.4
# <dbl> <dbl> <dbl> <dbl> <fct> <dbl>
# 1 5.1 3.5 1.4 0.2 setosa 0.8
# 2 4.9 3 1.4 0.2 setosa 0.8
# 3 4.7 3.2 1.3 0.2 setosa 0.8
# 4 4.6 3.1 1.5 0.2 setosa 0.8
# 5 5 3.6 1.4 0.2 setosa 0.8
# 6 5.4 3.9 1.7 0.4 setosa 1.6
# 7 4.6 3.4 1.4 0.3 setosa 1.2
# 8 5 3.4 1.5 0.2 setosa 0.8
# 9 4.4 2.9 1.4 0.2 setosa 0.8
#10 4.9 3.1 1.5 0.1 setosa 0.4
# … with 140 more rows
我们还可以传递带引号/不带引号的变量名作为列名来赋值。
multipetal <- function(df, name, n) {
mutate(df, {{name}} := Petal.Width * n)
}
multipetal(iris1, temp, 3)
# A tibble: 150 x 6
# Sepal.Length Sepal.Width Petal.Length Petal.Width Species temp
# <dbl> <dbl> <dbl> <dbl> <fct> <dbl>
# 1 5.1 3.5 1.4 0.2 setosa 0.6
# 2 4.9 3 1.4 0.2 setosa 0.6
# 3 4.7 3.2 1.3 0.2 setosa 0.6
# 4 4.6 3.1 1.5 0.2 setosa 0.6
# 5 5 3.6 1.4 0.2 setosa 0.6
# 6 5.4 3.9 1.7 0.4 setosa 1.2
# 7 4.6 3.4 1.4 0.3 setosa 0.900
# 8 5 3.4 1.5 0.2 setosa 0.6
# 9 4.4 2.9 1.4 0.2 setosa 0.6
#10 4.9 3.1 1.5 0.1 setosa 0.3
# … with 140 more rows
这是一样的
multipetal(iris1, "temp", 3)
经过大量的尝试和错误之后,我发现模式UQ(rlang::sym(“这里有一些字符串”))对于处理字符串和dplyr动词非常有用。它似乎在很多令人惊讶的情况下都起作用。
这是一个关于变异的例子。我们想要创建一个将两个列相加的函数,将两个列名作为字符串传递给函数。我们可以使用此模式和赋值操作符:=来完成此操作。
## Take column `name1`, add it to column `name2`, and call the result `new_name`
mutate_values <- function(new_name, name1, name2){
mtcars %>%
mutate(UQ(rlang::sym(new_name)) := UQ(rlang::sym(name1)) + UQ(rlang::sym(name2)))
}
mutate_values('test', 'mpg', 'cyl')
该模式也适用于其他dplyr函数。过滤器:
## filter a column by a value
filter_values <- function(name, value){
mtcars %>%
filter(UQ(rlang::sym(name)) != value)
}
filter_values('gear', 4)
或安排:
## transform a variable and then sort by it
arrange_values <- function(name, transform){
mtcars %>%
arrange(UQ(rlang::sym(name)) %>% UQ(rlang::sym(transform)))
}
arrange_values('mpg', 'sin')
对于select,您不需要使用模式。相反,你可以使用!!:
## select a column
select_name <- function(name){
mtcars %>%
select(!!name)
}
select_name('mpg')
虽然我喜欢将dplyr用于交互用途,但我发现使用dplyr来实现这一点非常棘手,因为您必须通过圆环来使用lazyeval::interp()、setNames等变通方法。
下面是一个使用基R的更简单的版本,至少对我来说,将循环放在函数内部似乎更直观,并且扩展了@MrFlicks的解决方案。
multipetal <- function(df, n) {
for (i in 1:n){
varname <- paste("petal", i , sep=".")
df[[varname]] <- with(df, Petal.Width * i)
}
df
}
multipetal(iris, 3)
另一种替代方法:在引号内使用{}来轻松创建动态名称。这与其他解决方案相似,但并不完全相同,而且我发现它更简单。
library(dplyr)
library(tibble)
iris <- as_tibble(iris)
multipetal <- function(df, n) {
df <- mutate(df, "petal.{n}" := Petal.Width * n) ## problem arises here
df
}
for(i in 2:5) {
iris <- multipetal(df=iris, n=i)
}
iris
我认为这来自dplyr 1.0.0,但不确定(如果重要的话,我也有rlang 4.7.0)。
这是另一个版本,可以说更简单一点。
multipetal <- function(df, n) {
varname <- paste("petal", n, sep=".")
df<-mutate_(df, .dots=setNames(paste0("Petal.Width*",n), varname))
df
}
for(i in 2:5) {
iris <- multipetal(df=iris, n=i)
}
> head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species petal.2 petal.3 petal.4 petal.5
1 5.1 3.5 1.4 0.2 setosa 0.4 0.6 0.8 1
2 4.9 3.0 1.4 0.2 setosa 0.4 0.6 0.8 1
3 4.7 3.2 1.3 0.2 setosa 0.4 0.6 0.8 1
4 4.6 3.1 1.5 0.2 setosa 0.4 0.6 0.8 1
5 5.0 3.6 1.4 0.2 setosa 0.4 0.6 0.8 1
6 5.4 3.9 1.7 0.4 setosa 0.8 1.2 1.6 2
你可能会喜欢package friendlyeval,它为新用户提供了一个简化的整洁的eval API和文档。
您正在创建希望更改为列名的字符串。所以使用friendlyeval你可以这样写:
multipetal <- function(df, n) {
varname <- paste("petal", n , sep=".")
df <- mutate(df, !!treat_string_as_col(varname) := Petal.Width * n)
df
}
for(i in 2:5) {
iris <- multipetal(df=iris, n=i)
}
它在底层调用rlang函数,检查varname作为列名是否合法。
friendlyeval代码可以在任何时候通过RStudio插件转换为同等的简单整洁的eval代码。