I just learned a new way to use 'length<-'
in an apply function
something like:
l<- list(c(1,1,2,2,2),c(1,2),c(1,2,2,2),c(1,2,2))
sapply(l, `length<-`, 6)
creates a 4x6 matrix,
[,1] [,2] [,3] [,4]
[1,] 1 1 1 1
[2,] 1 2 2 2
[3,] 2 NA 2 2
[4,] 2 NA 2 NA
[5,] 2 NA NA NA
[6,] NA NA NA NA
that is cool by itself. However I realized that
`length<-`(l[[1]],6)
will not change l[[1]]
, so that
lapply(l, `length<-`, 6)
will not change l
.
I would like to know if that is always the case for every single function that accepts a value assigned to it, such as 'dim<-'
, 'attr<-'
, etc. If so, is that a behaviour codified in the R parser?