0
votes

I have a long matrix (ncol=6 and nrow=50,000,000+). I want to transform it into a list of matrices where:

[i,4] and [i,5] are names (or keys) of the list,

[i,1] is the date,

[i,2] and [i,3] are time of the day,

[i,6] is the value to be stored in the list.

The matrices in the list are all be the same dimensions.

for (i in seq(1, nrow(a2015_15))) {
    OD_L_2015_15[[
        paste(a2015_15[i,4], a2015_15[i,5])
        ]] [
            a2015_15[i,1], paste(a2015_15[i,2], a2015_15[i,3]) 
            ] = 
        as.numeric(a2015_15[i,6])
}

As is, it takes an hour or two to run which is a pain especially on a slower computer.

Any suggestions to make this run quicker perhaps with an apply statement? I tried to limit the calls to a2015_15 (by saving a2015_15[i,] and then referencing it) but it didn't seem to help.

Thanks.

Edit: Here's the matrix I want to change:

Matrix

The list would be split into: OD_L_2015_15[[Key1,Key2]][Date,c(Time,Time2)]=Value

1
Can you provide a couple of example rows using dput? - 10 or so will do - just so we have something reproducible to play with.thelatemail
Are all values in all matrices "filled in"? For example, could you have a date and time-of day pair that has no associated value? If not, then the problem becomes much simpler.nograpes
Have you considered to use package data.table?Uwe

1 Answers

0
votes

The assignment operation appears to be vectorizeable, i.e. if it works for one value of i, it should also succeed for a vector of i's. . Have you tried just:

i <-  seq(1,nrow(a2015_15))
OD_L_2015_15 <- list()
OD_L_2015_15[[ paste(a2015_15[i,4], a2015_15[i,5])]] [
                                  a2015_15[i,1], paste(a2015_15[i,2], a2015_15[i,3]) ] <- 
as.numeric(a2015_15[i,6])

This is untested and I am relying on your affirmation that the loop succeeds. You should post a smaller version of this matrix if you want tested solutions since your description has no "grounding" in "reality" at that moment.