tracemem() Function in R (2 Examples) | Trace Copying of Data Objects

 

In this article, I’ll demonstrate how to apply the tracemem function in the R programming language.

The tutorial will contain the following content:

Let’s just jump right in:

 

Creation of Example Data

Before all else, we need to create some example data:

x <- 1:5                            # Create vector object
x                                   # Print vector object
# [1] 1 2 3 4 5

Have a look at the previous output of the RStudio console. It shows that our exemplifying data is a vector object containing a numeric range from the values 1 to 5.

 

Example 1: Create Copy of Vector with Same Memory Address

This example demonstrates how to construct a copy of our vector object that has the same memory address.

We can accomplish this by using the assignment operator a shown below:

x_copy1 <- x                        # Create copy of vector
x_copy1                             # Print copy of vector
# [1] 1 2 3 4 5

Now, we can apply the tracemem function to test if both vector objects share the same memory address:

tracemem(x) == tracemem(x_copy1)    # Check if memory addresses are the same
# [1] TRUE

The RStudio console returns the logical indicator TRUE, i.e. both data objects have the same memory address.

 

Example 2: Create Copy of Vector with Different Memory Address

The following R syntax illustrates how to duplicate a data object, but use a different memory address.

For this task, we can wrap the c function around our vector object as shown in the following R code:

x_copy2 <- c(x)                     # Create copy of vector
x_copy2                             # Print copy of vector
# [1] 1 2 3 4 5

Let’s apply the tracemem function once again:

tracemem(x) == tracemem(x_copy2)    # Check if memory addresses are the same
# [1] FALSE

This time, our comparison has revealed the logical indicator FALSE, i.e. the two vector objects do not share the same memory address.

 

Video & Further Resources

Would you like to learn more about the application of the tracemem function? Then you might want to watch the following video on my YouTube channel. I show the examples of this tutorial in the video:

 

The YouTube video will be added soon.

 

In addition to the video, you might want to read the related R articles on this website. You can find some tutorials below.

 

In summary: In this article you have learned how to use the tracemem function in the R programming language. In case you have additional questions, tell me about it in the comments. Besides that, please subscribe to my email newsletter for regular updates on new tutorials.

 

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.

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

Menu
Top