R: serialize base64 encode / decode text not exactly matching

in my previous question about using serialize () to create CSV objects, I got a great answer from jmoy, where he recommended encoding my serialized base64 text. This is exactly what I was looking for. Oddly enough, when I try to put this into practice, I get results that look right, but do not exactly match what I was looking at during serialization / coding.

The following example presents a list with 3 vectors and each vector is serialized. Then each vector is encoded with base64 and written to a text file along with the key. The key is simply the index number of the vector. Then I cancel the process and read each line back from csv. At the very end, you can see that some elements do not match exactly . Is this a floating point problem? Something else?

require(caTools)

randList <- NULL
set.seed(2)

randList[[1]] <- rnorm(100)
randList[[2]] <- rnorm(200)
randList[[3]] <- rnorm(300)

#delete file contents
fileName <- "/tmp/tmp.txt"
cat("", file=fileName, append=F)

i <- 1
for (item in randList) {
  myLine <- paste(i, ",", base64encode(serialize(item, NULL, ascii=T)), "\n", sep="")
  cat(myLine, file=fileName, append=T) 
  i <- i+1
}

linesIn <- readLines(fileName, n=-1)

parsedThing <- NULL
i <- 1
for (line in linesIn){
  parsedThing[[i]] <- unserialize(base64decode(strsplit(linesIn[[i]], split=",")[[1]][[2]], "raw"))
  i <- i+1
  }

#floating point issue?
identical(randList, parsedThing)

for (i in 1:length(randList[[1]])) {
  print(randList[[1]][[i]] == parsedThing[[1]][[i]])
}

i<-3
randList[[1]][[i]] == parsedThing[[1]][[i]]

randList[[1]][[i]]
parsedThing[[1]][[i]]

Here's the abbreviated output:

> #floating point issue?
> identical(randList, parsedThing)
[1] FALSE
> 
> for (i in 1:length(randList[[1]])) {
+   print(randList[[1]][[i]] == parsedThing[[1]][[i]])
+ }
[1] TRUE
[1] TRUE
[1] FALSE
[1] FALSE
[1] TRUE
[1] FALSE
[1] TRUE
[1] TRUE
[1] FALSE
[1] FALSE
...
> 
> i<-3
> randList[[1]][[i]] == parsedThing[[1]][[i]]
[1] FALSE
> 
> randList[[1]][[i]]
[1] 1.587845
> parsedThing[[1]][[i]]
[1] 1.587845
> 
+3
source share
3 answers

ascii=T serialize R - , . ascii=T, , , , .

base64encode , ascii=T.

, serialize, , .

: http://cran.r-project.org/doc/manuals/R-ints.html#Serialization-Formats

+2

JD: Linux-, , randList [[1]] [[i]] - parsedThing [[1]] [[i]].

, , . -4.440892-16, . .

, save/restore () . "" .

+2

, , , , ( , ).

(., , R FAQ),

  • identical()
  • all.equal()
  • RUnit, checkEquals

, base64, . . , , , , ...

+2
source

Source: https://habr.com/ru/post/1751774/


All Articles