Harshvardhan
Harshvardhan

Reputation: 559

Storing matrix after every iteration

I have following code.

for(i in 1:100)
{
   for(j in 1:100)
    R[i,j]=gcm(i,j)
}

gcm() is some function which returns a number based on the values of i and j and so, R has all values. But this calculation takes a lot of time. My machine's power was interrupted several times due to which I had to start over. Can somebody please help, how can I save R somewhere after every iteration, so as to be safe? Any help is highly appreciated.

Upvotes: 0

Views: 402

Answers (2)

Quentin Perrier
Quentin Perrier

Reputation: 546

You can use the saveRDS() function to save the result of each calculation in a file.

To understand the difference between save and saveRDS, here is a link I found useful. http://www.fromthebottomoftheheap.net/2012/04/01/saving-and-loading-r-objects/

Upvotes: 1

Bruno Zamengo
Bruno Zamengo

Reputation: 860

If you want to save the R-workspace have a look at ?save or ?save.image (use the first to save a subset of your objects, the second one to save your workspace in toto).

Your edited code should look like

for(i in 1:100)
{
   for(j in 1:100)
    R[i,j]=gcm(i,j)
    save.image(file="path/to/your/file.RData")
}

About your code taking a lot of time I would advise trying the ?apply function, which

Returns a vector or array or list of values obtained by applying a function to margins of an array or matrix

You want gmc to be run for-each cell, which means you want to apply it for each combination of row and column coordinates

R = 100; # number of rows
C = 100; # number of columns
M = expand.grid(1:R, 1:C); # Cartesian product of the coordinates
# each row of M contains the indexes of one of R's cells 
# head(M); # just to see it

# To use apply we need gmc to take into account one variable only (that' not entirely true, if you want to know how it really works have a look how at ?apply)
# thus I create a function which takes into account one row of M and tells gmc the first cell is the row index, the second cell is the column index
gmcWrapper = function(x) { return(gmc(x[1], x[2])); }

# run apply which will return a vector containing *all* the evaluated expressions
R = apply(M, 1, gmcWrapper); 

# re-shape R into a matrix
R = matrix(R, nrow=R, ncol=C);

If the apply-approach is again slow try considering the snowfall package which will allow you to follow the apply-approach using parallel computing. An introduction to snowfall usage can be found in this pdf, look at page 5 and 6 in particular

Upvotes: 0

Related Questions