Get startedGet started for free

References vs. Copies

1. References vs. Copies

As mentioned earlier, big matrices have been designed to look and feel like a regular R matrix.

2. Big matrices and matrices - Similarities

You can retrieve subsets of a big matrix just like you would a regular R matrix. You can also set values of a big matrix like you would with a regular R matrix.

3. Big matrices and matrices - Differences

However, it is sometimes important to realize that a big matrix isn't actually an R matrix. There are a couple of differences that can be important. One difference, which you already know, is that a big matrix object is normally stored on the disk, rather than RAM. This means that a big matrix can persist across R sessions and can even be shared among R sessions.

4. R usually makes copies during assignment

Another difference is that a big matrix object is not copied like a regular matrix. When an R variable is assigned to other variables it actually gets a copy of the content.

5. R usually makes copies during assignment

This is also true when parameters are passed to functions. Here we assign the value 42 to the variable a. Next, we create a function, foo(), that takes a single parameter, sets it to 43, and then outputs its value. When we call foo(a) The printed message tells us that a, inside the function is 43. When we print the value of a outside the function, it is still 42. This is because the function foo doesn't actually get a. It gets a copy of a. If we want to change the original a we need to do so outside of the function.

6. Not all R objects are copied

There are other types of variables, like environments, that are not copied when they are passed to a function. For these types it is the actual data structure and, as a result, if you change their values inside a function, you will see those changes after the function is finished executing. Those types of objects have reference semantics.

7. deepcopy()

A big matrix is a reference object. This means that assignments create new variables that point to the same data. If you want a copy you need to do it explicitly using the deepcopy() function.

8. Reference behaviour

Reference behavior ensures that R won't make copies of the matrix without you knowing. This helps minimize memory usage and execution time, but it also means you need to be careful when you are making changes.

9. Not all R objects are copied

Here we have an example that shows the reference behavior of a big matrix. We start by creating a new big matrix, called x.

10. Not all R objects are copied

We assign a new variable x_no_copy to x. x_no_copy now references the same data as x. If you update x, the changes will be reflected in x_no_copy as well.

11. Not all R objects are copied

However, when you create x_copy by explicitly applying deepcopy() on x, then x_copy has it's own copy of data in x. So if you change a value in x, it will not be reflected in x_copy.

12. Let's practice!

Now that you have seen the difference between copies and references, you get to spend a little time manipulating reference objects.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.