[R] loop is going to take 26 hours - needs to be quicker!

Rainer M Krug RKrug at sun.ac.za
Thu Dec 14 14:46:07 CET 2006


David Barron wrote:
> What about
> 
> gpcc.array <- array(gpcc.data2[,5], dim=c(144,72,46))

I guess this will be slightly faster then my suggestion :-) ?

> 
> On 14/12/06, Rainer M Krug <RKrug at sun.ac.za> wrote:
>> Jenny Barnes wrote:
>> > Dear R-help,
>> >
>> > I have a loop, which is set to take about 26 hours to run at the 
>> rate it's going
>> > - this is ridiculous and I really need your help to find a more 
>> efficient way of
>> > loading up my array gpcc.array:
>> >
>> > #My data is stored in a table format with all the data in one long 
>> column
>> > #running though every longitute, for every latitude, for every year. 
>> The
>> > #original data is sotred as gpcc.data2 where dim(gpcc.data2) = 
>> [476928,5] where
>> > #the 5th column is the data:
>> >
>> > #make the array in the format I need [longitude,latitude,years]
>> >
>> > gpcc.array <- array(NA, c(144,72,46))
>> >
>> > n=0
>> > for(k in 1:46){
>> > for(j in 1:72){
>> > for(i in 1:144){
>> > n <- n+1
>> > gpcc.array[i,j,k] <- gpcc.data2[n,5]
>> > print(j)
>> > }
>> > }
>> > }
>>
>> I don't know if it is faster - but adding three columns to qpcc.data,
>> one for longitude, one for lattitude and one for year (using rep() as
>> they are in sequence) and the using reshape() might be faster?
>>
>>
>> >
>> > So it runs through all the longs for every lat for every year - 
>> which is the
>> > order the data is running down the column in gpcc.data2 so n 
>> increses by 1 each
>> > time and each data point is pulled off....
>> >
>> > It needs to be a lot quicker, I'd appreciate any ideas!
>> >
>> > Many thanks for taking time to read this,
>> >
>> > Jenny Barnes
>> >


-- 
Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation
Biology (UCT)

Department of Conservation Ecology and Entomology
University of Stellenbosch
Matieland 7602
South Africa

Tel:		+27 - (0)72 808 2975 (w)
Fax:		+27 - (0)86 516 2782
Fax:		+27 - (0)21 808 3304 (w)
Cell:		+27 - (0)83 9479 042

email:	RKrug at sun.ac.za
       	Rainer at krugs.de



More information about the R-help mailing list