# tracemem and list of list

Hi all, I am bit of a beginner although I have dabbled in R before. Trying to learn the nitty gritty of R by following Advanced R, 2nd ed. (Wickham).

I was attempting example 4 in page 29. Please find the code below:

x <- list(1:10)
x[[2]] <- x

The explanation of the solution of this example can be found at https://advanced-r-solutions.rbind.io/names-and-values.html

However, I tried to use tracemem to explore how copy-on-modify works. So I added a few more lines in between, as shown below:

x <- list(1:10)
tracemem(x)
ref(x)
x[[2]] <- x
ref(x)

The output I get is as follows:

x <- list(1:10)
tracemem(x)
[1] "<0x5600392d0218>"
ref(x)
tracemem[0x5600392d0218 -> 0x560037beec08]: FUN lapply ref
█ [1:0x5600392d0218]
└─[2:0x56003918d610]
x[[2]] <- x
tracemem[0x5600392d0218 -> 0x5600394c2598]:
ref(x)
tracemem[0x5600392d0218 -> 0x5600380a5988]: FUN lapply FUN lapply ref
█ [1:0x5600377d72e0]
├─[2:0x56003918d610]
└─█ [3:0x5600392d0218]
└─[2:0x56003918d610]

• From my understanding, the first list is at address <0x5600392d0218>. But when ref is called on x initially, there is a new address at 0x560037beec08. However, the initial "x" was not modified in any way, so what does this new address denote? I read somewhere in the book that it might be due to some sort of referencing done in the environment pane?

• I am particularly confused, since in the very next line (i.e. the tree structure) shows the list being stored at mem addr 0x5600392d0218 (consistent with the initial address of the list) and the integer sequence at 0x56003918d610. So am I missing something?

After the following operation was carried out:

x[[2]] <- x

• tracemem indicates that a new list object has been created at address 0x5600394c2598. However, after calling ref(x), tracemem indicates that the new list was created at 0x5600380a5988? Again, I am a bit lost on what were held at 0x5600394c2598 and 0x5600380a5988.

• I was further confused by the first line of the tree, which is showing the new list at 0x5600377d72e0. So whatever happened to the list objects at 0x5600394c2598 and 0x5600380a5988? To me, the third line which points to the address of the first list (at 0x5600392d0218]) and the mem address of the integer sequence (0x56003918d610), makes sense. However, what's going on in the middle?

Also, the output I received when I printed off x is as follows:

[[1]]
[1]  1  2  3  4  5  6  7  8  9 10

[[2]]
[[2]][[1]]
[1]  1  2  3  4  5  6  7  8  9 10

I understand that to access elements of a list, [] is needed. However, what does [[2]][[1]] mean (in the second line, after [[2]]?

I tried to output individual elements as follows and got slightly different results:

> x[1]
[[1]]
[1]  1  2  3  4  5  6  7  8  9 10

> x[[1]]
[1]  1  2  3  4  5  6  7  8  9 10

> x[2]
[[1]]
[[1]][[1]]
[1]  1  2  3  4  5  6  7  8  9 10

> x[[2]]
[[1]]
[1]  1  2  3  4  5  6  7  8  9 10

I could not find any clear explanation of these differences. And compared to the output of x, these individual outputs are slightly different (in the indexing?). Any reason for that?

Sorry if my questions come off as naive, but I would grealty appreciate your help. Thanks!

Please see below for the version or R and Rstudio I am using:

platform x86_64-pc-linux-gnu
arch x86_64
os linux-gnu
system x86_64, linux-gnu
status
major 3
minor 4.4
year 2018
month 03
day 15
svn rev 74408
language R
version.string R version 3.4.4 (2018-03-15)
nickname Someone to Lean On

P.s. Please find attached, the diagram of the explanation from the solutions website.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.