Rename Data Object in R (2 Examples)

 

In this article, I’ll show how to change the name of a data object in the R programming language.

Table of contents:

Let’s dive right into the examples!

 

Introduction of Example Data

At first, we’ll need to create some example data:

my_data <- data.frame(x1 = 1:5,                         # Create data frame
                      x2 = letters[6:2],
                      x3 = "x")
my_data                                                 # Print data frame

 

table 1 data frame rename data object

 

As you can see based on Table 1, the exemplifying data is a data frame having five rows and three columns.

 

Example 1: Change Name of Data Frame Object

In Example 1, I’ll show how to rename our data frame object in R.

For this, we can simply create a new data object as a duplicate of our old data object using the assignment operator:

new_data <- my_data                                     # Rename data frame object
new_data                                                # Print renamed data frame object

 

table 2 data frame rename data object

 

By executing the previous R code we have created Table 2, i.e. a data frame called new_data that contains exactly the same values and columns as our input data frame.

Note that our data frame exists twice at this point (i.e. once with the name my_data and once with the name new_data). In the next example, I’ll explain how to change that!

 

Example 2: Remove Old Data Frame Object from Workspace

In Example 2, I’ll illustrate how to delete our old data frame from our global environment in RStudio.

For this, we can apply the rm function to the name of our original data frame:

rm(my_data)                                             # Remove old data frame object

If we now try to print our old data frame to the RStudio console, we can see that it does not exist anymore:

my_data                                                 # Old data frame does not exist anymore
# Error: object 'my_data' not found

Looks good.

 

Video & Further Resources

Have a look at the following video of my YouTube channel. In the video, I’m explaining the R programming code of this article:

 

 

In addition, you may read some of the other tutorials of my website. I have published several tutorials on topics such as data objects, time objects, and data conversion:

 

In this R programming post you have learned how to rename a data frame object. Please note that we could apply the same type of R code to other data classes such as vectors or lists as well.

If you have further questions, don’t hesitate to let me know in the comments section. In addition, please subscribe to my email newsletter in order to get updates on the newest posts.

 

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