I have a list of entries:
z <- list(list(a=1),list(a=4),list(a=2))
and I'm trying to add fields to each of them. Alas no
lapply(z,function(l) l$b <- 1+l$a)
neither
for(l in z) l$b <- 1+l$a
changes z.
In this simple case, of course I can do
z <- lapply(z,function(l) c(list(b= 1+l$a),l))
but this quickly gets out of hand when lists have more nesting:
z <- list(list(a=list(b=1)),list(a=list(b=4)),list(a=list(b=2)))
How to turn it into
list(list(a=list(b=1,c=2)),list(a=list(b=4,c=5)),list(a=list(b=2,c=3)))
without repeating the definition of the whole structure? Each element zhas many fields, and not only a; and z[[10]]$ahas many subfields, not just that b.
source
share