T_stats_3
T_stats_3

Reputation: 155

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?

Upvotes: 4

Views: 8139

Answers (2)

Pitagoras
Pitagoras

Reputation: 11

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

save(x, file="something.rda")
load("something.rda")

Upvotes: 1

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193587

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

Upvotes: 7

Related Questions