Home > other >  How to save a numerical matrix and restore it accurately?
How to save a numerical matrix and restore it accurately?

Time:10-08

I want to save a numerical matrix and then restore it accurately. But if I use identical() to compare the before- and after-saved matrices, they are not identical. I guess the problem is caused by floating point precision issues. How can I make the two matrices identical?

Thanks!

options(digits = 10)

data <-
  c(1 / 11, 1 / 22, 1 / 33, 1 / 44, 1 / 55, 1 / 66, 1 / 77, 1 / 88, 1 / 99) # Generate a numerical matrix.

x <- matrix(data,
            nrow = 3,
            ncol = 3,
            byrow = TRUE)

write.table(x, "test.csv") # Save the matrix.

y <- as.matrix(read.table("test.csv")) # Restore the matrix.

y <- unname(y) # Remove the attributes.

all.equal(x, y) # I got TRUE.

identical(x, y) # I got FALSE. How can I get TRUE here?

unlink("test.csv")

CodePudding user response:

all.equal has a tolerance parameter whose default allows for the values being compared to be very near equal but not exactly so while identical requires exact equality.

If you'd like the restored file to be identical, you can save as an RDS rather than a csv. Saving as an RDS also preserved other attributes of an R object that saving as a csv does not, allowing you to avoid converting the object back to the desired type after read in.

options(digits = 10)

data <-
  c(1 / 11, 1 / 22, 1 / 33, 1 / 44, 1 / 55, 1 / 66, 1 / 77, 1 / 88, 1 / 99) # Generate a numerical matrix.

x <- matrix(data,
            nrow = 3,
            ncol = 3,
            byrow = TRUE)

saveRDS(x, "test.RDS")

y <- readRDS( "test.RDS") # Restore the matrix.

all.equal(x, y) 
#> [1] TRUE

identical(x, y) 
#> [1] TRUE

unlink("test.RDS")
  • Related