1 min read

Rename a Data Frame Within a Function Passing an Argument

andreacirilloac

This is not actually a real post but rather a code snippet surrounded by text.

Nevertheless I think it is a quite useful one: have you ever found yourself writing a function where a data frame is created, wanting to name that data frame based on a custom argument passed to the function?

For instance, the output of your function is a really nice data frame name in a really trivial way, like “result”.

But your dream is to let the user (or some piece of code behind the function) specify the data frame name, passing it as an argument of your function.

To achieve that you need to look at _assign _function, which let’s you access the hash table of a given environment (_pos _argument) and change the value of a given variable.

Find below a working function which applies this idea:

[code language=“r”] rename_df  =  function(choosen_name){ data_set  =  data.frame(column_A = c(1,4,6,7,8),column_B = c(seq(1:5))) title  =  choosen_name assign(title,data_set,pos = “.GlobalEnv”) } [/code]

Have you found any other way to get here? I would love to here it!

p.s.: wondering why I chose that image? well, is Adam naming animals.. :)

comments disclaimer
thank you for taking the time to comment. If the comment you are about to write is related to a piece of code I wrote, please reach its Github respository and place there any request of improvement or report of bugs.