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.
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) .
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.
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.
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.
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) }
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.
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.
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)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With