Skip to content
Prev 350827 / 398502 Next

how to update a value in a list with lapply

On 2015-05-10 21:14, David Winsemius wrote:

            
This used to be true, but is no longer the case (from R-3.1.0 if my
memory serves me right).  Consider the following example.
[[1]]
[1] 1 2

[[2]]
[1] 3
@1fecce8 19 VECSXP g1c2 [MARK,NAM(1)] (len=2, tl=0)
   @26996a8 13 INTSXP g1c1 [MARK] (len=2, tl=0) 1,2
   @2699648 14 REALSXP g1c1 [MARK,NAM(2)] (len=1, tl=0) 3

Note the memory addresses (the ones starting with @).  Now, let's modify
the first list element:
@1fecce8 19 VECSXP g1c2 [MARK,NAM(1)] (len=2, tl=0)
   @26996a8 13 INTSXP g1c1 [MARK] (len=2, tl=0) 4,2
   @2699648 14 REALSXP g1c1 [MARK,NAM(2)] (len=1, tl=0) 3

As you can see, 'lst' changed but the memory address is still the same. 
  However, if we assign a double instead of an integer in the same 
position, copying must take place since the internal representation is 
changed:
@1fecce8 19 VECSXP g1c2 [MARK,NAM(1)] (len=2, tl=0)
   @12685a0 14 REALSXP g0c2 [] (len=2, tl=0) 4,2
   @2699648 14 REALSXP g1c1 [MARK,NAM(2)] (len=1, tl=0) 3

But note that *only* the address of the first list element changed. 
This list itself and the second list element remain at the same 
addresses as before.


Henric Winell