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? Best Sigbert
serialize does not work as expected
8 messages · Jeff Newmiller, Duncan Murdoch, Sigbert Klinke +3 more
Did you really conclude from looking at class that they were identical? Numeric mode sometimes makes it hard to distinguish integers from doubles, but they are different.
On August 29, 2020 8:34:29 AM PDT, Sigbert Klinke <sigbert at wiwi.hu-berlin.de> 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? Best Sigbert
Sent from my phone. Please excuse my brevity.
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
Hi, is there in R a way to "normalize" a vector from compact_intseq/compact_realseq to a "normal" vector? Sigbert Am 29.08.20 um 18:13 schrieb Duncan Murdoch:
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
Does serialize(..., version = 2L) do what you want? /Henrik On Sat, Aug 29, 2020 at 10:10 AM Sigbert Klinke
<sigbert at wiwi.hu-berlin.de> wrote:
Hi, is there in R a way to "normalize" a vector from compact_intseq/compact_realseq to a "normal" vector? Sigbert Am 29.08.20 um 18:13 schrieb Duncan Murdoch:
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
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
For some reason l[[2]] is serialized as a 'compact_realseq' and l[3]] is not. They both unserialize to the same thing. On Windows I get:
lapply(l, function(x)rawToChar(serialize(x, connection=NULL, ascii=TRUE)))
[[1]] [1] "A\n3\n262146\n197888\n6\nCP1252\n238\n2\n1\n262153\n14\ncompact_intseq\n2\n1\n262153\n4\nbase\n2\n13\n1\n13\n254\n14\n3\n3\n1\n1\n254\n" [[2]] [1] "A\n3\n262146\n197888\n6\nCP1252\n238\n2\n1\n262153\n15\ncompact_realseq\n2\n1\n262153\n4\nbase\n2\n13\n1\n14\n254\n14\n3\n3\n1\n1\n254\n" [[3]] [1] "A\n3\n262146\n197888\n6\nCP1252\n14\n3\n1\n2\n3\n" Bill Dunlap TIBCO Software wdunlap tibco.com On Sat, Aug 29, 2020 at 8:37 AM Sigbert Klinke
<sigbert at wiwi.hu-berlin.de> 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? Best Sigbert -- https://hu.berlin/sk https://hu.berlin/mmstat3
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
On 29/08/2020 1:10 p.m., Sigbert Klinke wrote:
Hi, is there in R a way to "normalize" a vector from compact_intseq/compact_realseq to a "normal" vector?
I don't know if there's a function specifically designed to do that, but as Henrik proposed, this works: l_normalized <- unserialize(serialize(l, connection=NULL, version=2)) Duncan Murdoch
compact sequences are actually an ALTREP object. I do not know if there is any standard way to do it, but here is a trick for what you want. ```
x <- 1:3 .Internal(inspect(x))
@0x00000196bed8dd78 13 INTSXP g0c0 [NAM(7)] 1 : 3 (compact)
x[1] <- x[1] .Internal(inspect(x))
@0x00000196bef90b60 13 INTSXP g0c2 [NAM(7)] (len=3, tl=0) 1,2,3 ``` Best, Jiefei On Sat, Aug 29, 2020 at 1:10 PM Sigbert Klinke <sigbert at wiwi.hu-berlin.de> wrote:
Hi, is there in R a way to "normalize" a vector from compact_intseq/compact_realseq to a "normal" vector? Sigbert Am 29.08.20 um 18:13 schrieb Duncan Murdoch:
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
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.