[R] question on avoiding the use of loops when working with large multidimensional arrays

Cheney Shreve cmshreve at gmail.com
Tue Dec 29 18:02:10 CET 2009


Hi,

  I’m new to R and object-oriented programming. I work primarily with  
satellite datasets using IDL. I used IDL to ingest my data, run a  
spectral mixture algorithm and output a timeseries of a snow index in  
16-bit integer band interleaved by line format (for most of China). In  
R, I read in the timeseries using the caTools package and my goal is  
to figure out how to extract slices of my data cube (details on  
dimensions below) and apply functions written in R without the use of  
a loop. The code below is an example of what I’m doing now and I’m  
sure there must be a better way. Thank you in advance for your help,  
which is greatly appreciated. I have spent some time with the R help  
materials, but unfortunately the learning curve is still steep my  
first week into R.

# Reading in my data using caTools
 >mydata=read.ENVI("rsma_subset_3s.bil",headerfile=paste 
("rsma_subset_3s.bil",".hdr",sep=""))
 > dim(mydata)
[1]  37 103 208
 > # "my data" is a 16-bit Integer in Band Interleaved by Line (bil)  
format
 > # mydata [x,y,z] is latitude,longitude,value of an index per band  
(time)
 > # what I want to do is create a "ts" object and run a function  
called "bfast"
 > # on slices of the array without going through a loop because the  
actual size the of data
 > # is [2400,1200,208] and looping takes a long time

  # The example loop below will run but takes a long time and I have  
numbers hardwired # in to loop over, which I’d like to avoid
 > for (i in 1:37) {
+  for (j in 1:103)
+  # create "ts" object with proper time, frequency
+  slice <- ts(mydata[i,j,1:208],start=c(2000,4),end=c 
(2009,4),frequency=23);
+  fit <- bfast(slice,h=0.15,max.iter=1);
+  plot(fit) # just plotting to see if it’s working, but I will  
actually be extracting
+ # variables out of  ‘fit’
+ }
}
# I would also like to replace the numeric values of columns, rows,  
bands using
# “length” so that in the future, I don’t have numbers hardwired in  
but I don’t think I’m
# doing this correctly for a multidimensional array. Can the  
“dimnames” be used as an
# alternate to do this?


Kind regards,
Cheney



More information about the R-help mailing list