Create Copy of Data Frame in R (2 Examples) | Same & Different Memory Address
In this tutorial, I’ll show how to create a copy of a data frame object in R programming.
The page looks as follows:
If you want to learn more about these content blocks, keep reading…
Introducing Example Data
The first step is to create some example data:
data <- data.frame(x1 = 6:1, # Create example data x2 = "x", x3 = LETTERS[1:6]) data # Print example data

As you can see based on Table 1, the example data is a data frame containing six rows and three columns. The column x1 is an integer and the columns x2 and x3 have the character class.
Example 1: Create Copy of Data with Same Memory Address
In Example 1, I’ll show how to construct a duplicate of our data frame with the same memory address.
To accomplish this, we can simply use an assignment arrow as shown below:
data_copy1 <- data # Create copy of data data_copy1 # Print copy of data

After executing the previous R code the data frame shown in Table 2 has been created. As you can see, this data frame contains exactly the same values as our input data frame.
We can now use the tracemem function to test whether both of our data frames have the same memory address:
tracemem(data) == tracemem(data_copy1) # Check if memory addresses are the same # [1] TRUE
The previous R code has returned the logical indicator TRUE, i.e. both data sets have the same memory address.
Example 2: Create Copy of Data with Different Memory Address
Example 2 illustrates how to replicate a data frame with a different memory address.
For this task, we have to wrap the data.frame function around our input data set:
data_copy2 <- data.frame(data) # Create copy of data data_copy2 # Print copy of data

In Table 3 it is shown that we have created another version of our data frame using the previous R programming syntax.
Let’s test if the memory address is still identical:
tracemem(data) == tracemem(data_copy2) # Check if memory addresses are the same # [1] FALSE
The RStudio console returns the logical value FALSE, i.e. the two data sets do not have the same memory address.
Video & Further Resources
If you need more explanations on the contents of this page, you might watch the following video on my YouTube channel. I explain the R syntax of this article in the video:
Additionally, you might have a look at some of the related RStudio tutorials on this website. I have published several articles already:
- Create Duplicate of Column in R
- Remove Columns with Duplicate Names from Data Frame
- Delete Duplicate Rows Based On Column Values
- duplicated Function in R
- unique Function in R
- How to Construct a Data Frame in R
- Introduction to R Programming
Summary: This article has demonstrated how to construct a copy of a data frame object in R. If you have further questions, don’t hesitate to let me know in the comments below.
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.
Thank you!
Welcome to the Statistics Globe newsletter. From now on, I’ll send you regular emails about statistics, data science, AI, and programming with R and Python.
I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.
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.
Thank you!
Please check your email inbox and click the confirmation link to complete your subscription. If you don’t see the email within a few minutes, please also check your spam/junk folder.







4 Comments. Leave new
When should you copy to the same memory address and when shouldn’t you?
If you copy to the same memory address, why isn’t the original data frame changed?
Many thanks, Peter
Hi Peter,
Copying to the same memory address is useful when you want to create a reference to the original data frame without using additional memory. This is efficient but means any modifications to the copy will also affect the original.
On the other hand, copying to a different memory address is necessary when you want to work with an independent version of the data frame without altering the original.
The original data frame isn’t changed when copying to the same memory address unless modifications are made. R uses copy-on-modify behavior, meaning a true copy is only created when changes occur.
I hope this helps!
Joachim
Many thanks Joachim.
If I create a second data frame (df2), based on the filtering on the first data frame (df1) from my reading of https://bookdown.dongzhuoer.com/hadley/adv-r/copy-on-modify, I would assume that subsequent modifications to df2 would not affect df1 as filter is making a row selection/change a row. Is that the case?
Many thanks,
Peter
You are welcome. Could you please share your code?
Regards,
Joachim