[R] serialize does not work as expected
Duncan Murdoch
murdoch@dunc@n @end|ng |rom gm@||@com
Sat Aug 29 18:13:12 CEST 2020
On 29/08/2020 11:34 a.m., Sigbert Klinke wrote:
> Hi,
>
> if I create a list with
>
> l <- list(1:3, as.numeric(1:3), c(1,2,3))
>
> and applying
>
> lapply(l, 'class')
> lapply(l, 'mode')
> lapply(l, 'storage.mode')
> lapply(l, 'typeof')
> identical(l[[2]], l[[3]])
>
> then I would believe that as,numeric(1:3) and c(1,2,3) are identical
> objects. However,
>
> lapply(l, serialize, connection=NULL)
>
> returns different results for each list element :(
>
> Any ideas, why it is like that?
Objects like 1:3 are stored in a special compact form, where 1:3 takes
up the same space as 1:1000000. Apparently as.numeric() knows to work
with that special form, and produces the numeric version of it.
You can confirm this by looking at the results of
serialize(l[[i]], connection=stdout(), ascii=TRUE)
for each of i=1,2,3:
> for (i in 1:3) {
+ cat("\nElement", i, "\n")
+ serialize(l[[i]], connection=stdout(), ascii=TRUE)
+ }
Element 1
A
3
262146
197888
5
UTF-8
238
2
1
262153
14
compact_intseq
2
1
262153
4
base
2
13
1
13
254
14
3
3
1
1
254
Element 2
A
3
262146
197888
5
UTF-8
238
2
1
262153
15
compact_realseq
2
1
262153
4
base
2
13
1
14
254
14
3
3
1
1
254
Element 3
A
3
262146
197888
5
UTF-8
14
3
1
2
3
Notice how element 1 is a "compact_intseq" and element 2 is a
"compact_realseq".
Duncan Murdoch
More information about the R-help
mailing list