Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to rename a variable using a dynamic name and dplyr?

Tags:

r

dplyr

nse

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

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.

like image 615
Malta Avatar asked Apr 04 '18 11:04

Malta


People also ask

How do I rename a variable in dplyr?

To rename a column in R you can use the rename() function from dplyr. For example, if you want to rename the column “A” to “B”, again, you can run the following code: rename(dataframe, B = A) .

How do I change a variable name in R studio?

It is achieved by selecting the function or variable we want to change and pressing Ctrl + Shift + Alt + M. It will select all occurrences in scope, you will have to just type a new name.

Is it possible to rename the variable?

Rename A Variable In Power AppsStart by choosing a variable name to change. In this example we will use the variable name EditRecord. Go to the Search menu and search for EditRecord. Then write the new variable name gblCurrentRecord in the replace with field.

How to rename the column name using dplyr in R?

In this article, we are going to rename the column name using dplyr package in the R programming language. rename (dataframe,new_columnname=old_column,………….,name,new_columnname=old_columnname) Where dataframe is the input dataframe, new_columnname is the newname of the column and old_columnname is the old name of the column.

How to dynamically assign parameter names in dplyr?

dplyr starting with version 0.7 allows you to use := to dynamically assign parameter names. You can write your function as: # --- dplyr version 0.7+--- multipetal <- function (df, n) { varname <- paste ("petal", n , sep=".") mutate (df, !!varname := Petal.Width * n) }

How do I change the column names of certain variables?

As you can see based on the previous output of the RStudio console, the select function returned a subset of our original data frame containing only the two selected columns. With the rename function, we can change the column names of certain variables. Note that it is important to write dplyr:: in front of the rename function.

How do you rename a column in R with a function?

With the rename function, we can change the column names of certain variables. Note that it is important to write dplyr:: in front of the rename function. Several R packages contain a rename function and with dplyr:: we tell R to use the rename function of the dplyr package.


1 Answers

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).

Or you can use{{...}}

produce_data_frame <- function(name) {
  iris %>%
    as_tibble() %>% 
    select({{name}} := Sepal.Length)
}
like image 114
Roman Avatar answered Oct 06 '22 11:10

Roman