Rename Object or Variable in R

To rename a data object or a variable name in R, you can use two approaches, first change the existing variable to a new name, and second copy it to a new variable and delete/drop the existing one.

You can use these approaches to rename objects DataFrame, Vector, list, and matrices in the R programming language.

First, let’s create a DataFrame.


# Create dataframe
df=data.frame(id=c(11,22,33),
              pages=c(32,45,33),
              name=c("spark","python","R"),
              chapters=c(76,86,11),
              price=c(144,553,321))
print(df)

Yields below output.

r rename varialbe

1. Rename Object or Variable in R

To rename an object or variable in R, just copy the data.frame variable to another variable by using the assignment operator (<- or =) and remove the existing variable using rm() function.

1.1 Copy the Variable to Another

When you copy the DataFrame variable to another, R is smart enough not to make a copy of the variable instead the new variable just refers to the same memory address. You can use tracemem() to get the address of the variable to verify.


# Copy to a new variable
new_df <- df
tracemem(new_df)

Yields below output.

r rename object

Note that R refers to the same memory address for both variables and it creates a new copy in the memory if one of them is modified, After the update, the 2 objects are not identical anymore and they both have different memory addresses.

1.2 Remove the Existing or Old Object

Now we have DataFrame objects df and new_df pointing to the same memory address, you can remove the existing old df object, this completes the renamed variable.


# Remove old variable
rm(df)
tracemem(new_df)

Yields below output. Note that even after removing the old variable, the new variable new_df still has the same memory. This proves that when you rename the variable, R doesn’t copy the data to a new variable instead it just drops the memory link to the old variable.

r rename variable data.frame

2. Rename Vector Object or Variable

Let’s see another example of how to rename a vector variable in R. The procedure would be exactly the same as data.frame.


# Create Vector using c()
id <- c(10,11,12,13)

# Copy to a new variable
new_id <- id
tracemem(new_id)

# Remove old variable
rm(id)
tracemem(new_id)

3. Rename Object using mv()

As you learned creating the intermediate or temporary variable to rename is not a bad idea as it doesn’t copy the data however, if you wanted to rename the variable by not creating the temporary variable use mv() function from gdata package.


# Rename variable
library(gdata)

tracemem(df)
"<0x15a5b6d78>"

mv(from = "df", to = "df2")

tracemem(df2)
"<0x15a5b6d78>"

Observe that the output of tracemem() function of both objects is the same. This mv() function internally assigns the object to the environment in scope and then removes the old object.

4. Conclusion

In this article, you have learned how to rename an R object or variable by using the mv() function from the gdata package and using the assignment operator to copy the object and delete the existing variable by using the rm() function. When R makes a copy of an object it internally creates a “soft link” the object is not actually copied but simply linked to another name.

Related Articles

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

You are currently viewing Rename Object or Variable in R