[R] Help with nested loops

tsippel tsippel at gmail.com
Wed Apr 15 00:26:44 CEST 2009


Hello-
I need to loop through a directory of files to extract data corresponding to
dates in a dataframe.  Within a function, I've written nested loops to index
the dataframe dates, and the directory files.  My function successfully
extracts the data corresponding to my first data frame date, but stops there
and doesn't continue through my entire list of data frame dates and
directory files (I need it to go through ~2600 lines in my data frame, and
~50 data files in my directory).

Currently, I'm using an 'if' statement to see if my data frame dates and
directory file names meet the criteria for data extraction, which is I think
why it stops after the first successful iteration. However, I'm stumped on
what to do now.  I have tried using 'while' statements which seem to hang up
in an endless loop.  

Some advice on getting the function to finish looping through all of my data
frame lines and directory files would be much appreciated.  I've also read
that apply, tapply, etc. can be more efficient at looping tasks, but my
experimenting with them has not been much help and I'm not sure my task is
best suited to those functions. 

Below is an example. I'm using R 2.7.2 in Vista.

#### Example####
extract.asc.data.for.tracks<-function(id, d.frame.date, centroid.x,
centroid.y, ci.x, ci.y, asc.dir, asc.date.start,
  asc.date.end, asc.duration, env.variable) {
  id<-id 
  d.frame.date<-as.POSIXct(d.frame.date, "GMT", format="%Y-%m-%d")
  require(RSAGA)
  # set environment for RSAGA
  env<-rsaga.env(path="C:\\programs\\saga_vc", cmd="saga_cmd.exe")
  # format directory file names for input into data extraction function
below called 'pick.from.ascii.grid'
  asc.files<-substr(basename(dir(asc.dir, pattern='.asc$')), 1,
nchar(basename(dir(asc.dir, pattern='.asc$'))) - 4) 
  # index and loop through dates dataframe
  for (j in 1 : length(asc.files)){ 
    # index and loop through datafiles in directory  
    for (i in 1 : length(d.frame.date)){   
      # find dates within the named directory of files that are within 7
days of any given dataframe date
      if ((as.POSIXct(substr(asc.files[j], start = nchar(asc.files[j]) -
asc.date.start,
            stop = nchar(asc.files[j]) - asc.date.end), 'GMT') -
d.frame.date[i]) %in% 1:asc.duration)
        # when date criteria are met, extract data using coordinates(x and
y) plus a buffer (ci.x and ci.y)
        dat<-pick.from.ascii.grid(data=as.data.frame(cbind(x=centroid.x[i] +
ci.x, y=centroid.y[i] + ci.y)),  env=env, cbind=T,  
        path=asc.dir, file=paste(asc.files[j],'asc', sep="."), at.once=F,
method="nearest.neighbour", nodata.values=-9999999)
        # calculate weighted mean of extracted data
        w.mean<-weighted.mean(x=dat[,3], w=den.xt$y*den.yt$y, na.rm=T)        
  # return new data frame containing original id, date, x/y coords, and
newly calculated weighted mean
  out.df<-data.frame(id[i], d.frame.date[i], centroid.x[i], centroid.y[i],
w.mean[i])
  # give names to columns in new data frame
  names(out.df)<-c('id', 'date', 'x', 'y', paste(env.variable, 'w.mean',
sep="_"))  
  return(out.df)
  }}}
  
# subset of my data frame
> stm.data
         id                date        x         y
10 STM05_2 2005-03-01 12:00:00 178.2606 -34.82035
11 STM05_2 2005-03-02 00:00:00 178.2281 -34.38141
12 STM05_2 2005-03-02 12:00:00 178.2145 -33.95625
13 STM05_2 2005-03-03 00:00:00 178.2123 -33.55642
14 STM05_2 2005-03-03 12:00:00 178.2056 -33.18816
15 STM05_2 2005-03-04 00:00:00 178.1920 -32.85041
16 STM05_2 2005-03-04 12:00:00 178.1722 -32.54057
17 STM05_2 2005-03-05 00:00:00 178.1465 -32.25634
18 STM05_2 2005-03-05 12:00:00 178.1150 -31.99568
19 STM05_2 2005-03-06 00:00:00 178.0777 -31.75682

# some of the data files in directory which I need to extract data from
> asc.dir
[1] "tmp1_290E0N2005-03-02"  "tmp1_290E0N2006-01-05" 
"tmp1_290E0N2006-07-08"  "tmp1_290E0N2007-02-22"  "tmp10_290E0N2005-05-13"
"tmp10_290E0N2006-03-18"

#### Arguements to run the function
extract.asc.data.for.tracks(id=stm.data$id, d.frame.date=stm.data$date,
centroid.x=stm.data$x, centroid.y=stm.data$y, 
  ci.x=seq(-1,1,0.1), ci.y=seq(-1,1,0.1), asc.date.start=9, asc.date.end=0,
asc.duration=7, env.variable="SST",
  asc.dir="C:\\...\\data.files")

# the result of the function successfully extracting data from a single row
in my data frame, but I need to do this for ~ 2600 dataframe lines....
      id       date        x         y SST_w.mean
1 STM05_2 2005-03-01 178.2606 -34.82035   21.97327  


Many thanks,

Tim
-- 
View this message in context: http://www.nabble.com/Help-with-nested-loops-tp23049250p23049250.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list