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:

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")
}

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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s