Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to save a 3d array in R

I have a 3d array in R which takes quite some time to calculate.

I am looking for the easiest way to store this array so it can be read back into R quickly and in an array format.

Can anyone explain how to do this?

I have tried using

saveRDS(x_out, file="x_out.Rda")
x_out1 <- load(file="x_out.Rda")

but this leads to an error.

Error: bad restore file magic number (file may be corrupted) -- no data loaded
In addition: Warning message:
file ‘x_out.Rda’ has magic number 'X'
  Use of save versions prior to 2 is deprecated 

Any suggestions?

like image 398
T_stats_3 Avatar asked Oct 26 '25 22:10

T_stats_3


2 Answers

Bad magic file type errors are usually because you're trying to use the wrong function to read a particular file type.

The inverse of saveRDS is readRDS, not load.

Demo:

saveRDS(c(1:3), "test.rds")
x <- load("test.rds")
# 'Error: bad restore file magic number (file may be corrupted) -- no data loaded
# In addition: Warning message:
# file ‘test.rds’ has magic number 'X'
#   Use of save versions prior to 2 is deprecated 
x <- readRDS("test.rds")
x
# [1] 1 2 3
like image 133
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 29 '25 11:10

A5C1D2H2I1M1N2O1R2T1


I am saving and loading 3D numeric arrays just with save() and load() commands:

save(x, file="something.rda")
load("something.rda")
like image 23
Pitagoras Avatar answered Oct 29 '25 13:10

Pitagoras



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!