Convert Name of Data Object to Character String in R (Example)

 

This page shows how to return the name of a data object as a character string in the R programming language.

Table of contents:

So let’s do this!

 

Creation of Example Data

The first step is to create some data that we can use in the exemplifying code later on:

my_object <- 1:5                               # Create example data object
my_object                                      # Print example data object
# [1] 1 2 3 4 5

As you can see based on the previously shown output of the RStudio console, our example data object is a numeric vector ranging from 1 to 5.

However, in this tutorial we do not care about the content of our variable. Instead, we care about the variable name! So let’s convert our data object’s name to a character string.

 

Example: Get Object Name as Character String Using deparse() & substitute() Functions

This section shows how to extract the name of a data object as a character string. For this, we can use the deparse and substitute functions as shown below:

my_string <- deparse(substitute(my_object))    # Apply deparse & substitute
my_string                                      # Show object name as character string
# [1] "my_object"

The previous R code has created a new data object called my_string which is containing the character string “my_object”, i.e. the name of our input data.

Note that we have discussed a very simplified example here. However, it would be possible to use the same kind of R syntax in more complex settings, for example when using for-loops or user-defined functions.

 

Video & Further Resources

Do you need more information on the R code of this tutorial? Then I recommend watching the following video instruction that I have published on my YouTube channel. I explain the R programming codes of this tutorial in the video:

 

 

Besides that, you may have a look at the other tutorials on this homepage.

 

In this article, I have illustrated how to get a variable name as a character string in the R programming language. Let me know in the comments below, if you have any further questions. Furthermore, don’t forget to subscribe to my email newsletter in order to get updates on the newest articles.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top