Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R renaming passed columns in functions

Tags:

function

r

dplyr

I have been searching this and have found this link to be helpful with renaming passed columns from a function (the [,column_name] code actually made my_function1 work after I had been searching for a while. Is there a way to use the pipe operator to rename columns in a dataframe within a function?

My attempt is shown in my_function2 but it gives me an Error: All arguments to rename must be named or Error: Unknown variables: col2. I am guessing because I have not specified what col2 belongs to.

Also, is there a way to pass associated arguments into the function, like col1 and new_col1 so that you can associated the column name to be replaced and the column name that is replacing it. Thanks in advance!

library(dplyr)

my_df = data.frame(a = c(1,2,3), b = c(4,5,6), c = c(7,8,9))

my_function1 = function(input_df, col1, new_col1) {
  df_new = input_df
  df_new[,new_col1] = df_new[,col1]
  return(df_new)
}
temp1 = my_function1(my_df, "a", "new_a")

my_function2 = function(input_df, col2, new_col2) {
  df_new = input_df %>%
    rename(new_col2 = col2)
  return(df_new)
}

temp2 = my_function2(my_df, "b", "new_b")
like image 400
Prevost Avatar asked Jan 26 '16 20:01

Prevost


People also ask

How do I change multiple column names in R?

rename() is the method available in the dplyr library which is used to change the multiple columns (column names) by name in the dataframe. The operator – %>% is used to load the renamed column names to the dataframe. At a time it will change single or multiple column names.

How do I rename columns in R?

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) . That was it, we are getting ready to practice how to change the column names in R.

Is there a rename function in R?

rename() function in R Language is used to rename the column names of a data frame, based on the older names.

How do I permanently rename a column in R?

You just need to use the rename() function and supply the new names and old names with the structure new_name = old_name .

How to rename columns of a data frame in R?

Rename Columns of a Data Frame in R Programming – rename () Function Last Updated : 19 Jun, 2020 rename () function in R Language is used to rename the column names of a data frame, based on the older names. Syntax: rename (x, names)

How do I rename a column in R with dplyr?

Renaming Columns Using dplyr. Another way to rename columns in R is by using the rename () function in the dplyr package. The basic syntax for doing so is as follows: data %>% rename (new_name1 = old_name1, new_name2 = old_name2, ....)

Is there a way to rename a variable in R?

Syntactically, many tools and functions from “early R” are poorly named. And many methods of doing things are a little syntactically awkward. Renaming variables is no exception. Moreover, R has several different ways to rename variables in a dataframe.

How many column names can a function return in R?

As you can see, our function just returns the two specified column names. However, you may do whatever you want within your own user-written function. Would you like to know more about manually created functions in R?


2 Answers

rename_ (alongside other dyplyr verbs suffixed with an underscore) has been depreciated. Instead, try:

my_function3 = function(input_df, cols, new_cols) { 
  input_df %>%
    rename({{ new_cols }} := {{ cols }}) 
}

See this vignette for more information about embracing arguments with double braces and programming with dplyr.

like image 101
4redwood Avatar answered Oct 16 '22 08:10

4redwood


Following @MatthewPlourde's answer to a similar question, we can do:

my_function3 = function(input_df, cols, new_cols) { 
  rename_(input_df, .dots = setNames(cols, new_cols)) 
}

# example
my_function3(my_df, "b", "new_b")
#   a new_b c
# 1 1     4 7
# 2 2     5 8
# 3 3     6 9

Many dplyr functions have less-known variants with names ending in _. that allow you to work with the package more programmatically. One pattern is...

DF %>% dplyr_fun(arg1 = val1, arg2 = val2, ...)
# becomes
DF %>% dplyr_fun_(.dots = list(arg1 = "val1", arg2 = "val2", ...))

This has worked for me in a few cases, where the val* are just column names. There are more complicated patterns and techniques, covered in the document that pops up when you type vignette("nse"), but I do not know them well.

like image 3
Frank Avatar answered Oct 16 '22 09:10

Frank