如何使用动态名称和dplyr重命名变量?

时间:2022-05-20 23:06:25

I want to rename a column inside a function with a name passed as an argument for this function. Basically, I have a function

我想重命名一个函数内的列,其名称作为此函数的参数传递。基本上,我有一个功能

produce_data_frame <- function(name) {
  return(iris)
}

And I want that this function change the Sepal.length column name with 'name' (with name taking the value of name) I tried different things such as

我希望这个函数改变Sepal.length列名称'name'(名称取名称)我尝试了不同的东西,如

produce_data_frame <- function(name) {
  name <- enquo(name)
  iris %>%
    rename((!!name) = Sepal.Length) %>%
    return()
}

And when calling

而且在打电话时

produce_data_frame("newName")

I would like to get back the iris data.frame with the Sepal.Length column named newName. But my understanding of NSE is still very basic, and it doesn't even compile.

我想用名为newName的Sepal.Length列返回iris data.frame。但我对NSE的理解仍然非常基础,甚至无法编译。

3 个解决方案

#1


8  

You can try

你可以试试

library(tidyverse)

produce_data_frame <- function(name) {
  iris %>%
    as.tibble() %>% 
    select(!!quo_name(name) := Sepal.Length)
}

produce_data_frame("new_name")
#> # A tibble: 150 x 1
#>    new_name
#>       <dbl>
#>  1     5.10
#>  2     4.90
#>  3     4.70
#>  4     4.60
#>  5     5.00
#>  6     5.40
#>  7     4.60
#>  8     5.00
#>  9     4.40
#> 10     4.90
#> # ... with 140 more rows

Created on 2018-04-04 by the reprex package (v0.2.0).

由reprex包(v0.2.0)创建于2018-04-04。

#2


4  

You can use base R names() instead.

您可以使用基本R名称()代替。

produce_data_frame <- function(name) {
  temp_df <- iris
  names(temp_df)[names(temp_df) == "Sepal.Length"] <- name
  return(temp_df)
}

produce_data_frame("newName")
#        newName 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


2  

From Different input and output variable in the Programming with dplyr vignette, we can use the := operator:

从使用dplyr晕图编程中的不同输入和输出变量,我们可以使用:=运算符:

library(dplyr)
library(rlang)


produce_data_frame <- function(name) {
  name = quo_name(name)
  iris %>%
    rename(!!name := Sepal.Length)
}

produce_data_frame('test') %>% colnames()
#> [1] "test"         "Sepal.Width"  "Petal.Length" "Petal.Width" 
#> [5] "Species"

#1


8  

You can try

你可以试试

library(tidyverse)

produce_data_frame <- function(name) {
  iris %>%
    as.tibble() %>% 
    select(!!quo_name(name) := Sepal.Length)
}

produce_data_frame("new_name")
#> # A tibble: 150 x 1
#>    new_name
#>       <dbl>
#>  1     5.10
#>  2     4.90
#>  3     4.70
#>  4     4.60
#>  5     5.00
#>  6     5.40
#>  7     4.60
#>  8     5.00
#>  9     4.40
#> 10     4.90
#> # ... with 140 more rows

Created on 2018-04-04 by the reprex package (v0.2.0).

由reprex包(v0.2.0)创建于2018-04-04。

#2


4  

You can use base R names() instead.

您可以使用基本R名称()代替。

produce_data_frame <- function(name) {
  temp_df <- iris
  names(temp_df)[names(temp_df) == "Sepal.Length"] <- name
  return(temp_df)
}

produce_data_frame("newName")
#        newName 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


2  

From Different input and output variable in the Programming with dplyr vignette, we can use the := operator:

从使用dplyr晕图编程中的不同输入和输出变量,我们可以使用:=运算符:

library(dplyr)
library(rlang)


produce_data_frame <- function(name) {
  name = quo_name(name)
  iris %>%
    rename(!!name := Sepal.Length)
}

produce_data_frame('test') %>% colnames()
#> [1] "test"         "Sepal.Width"  "Petal.Length" "Petal.Width" 
#> [5] "Species"