I think something like the following will let you do the thing you want in R:
> vec1 <- 1:10
> vec2 <- 2:11
> vec3 <- 3:12
> fun <- function(x){
+ vec1[x] * vec2[x] * vec3[x] #whatever
+ }> tt <- lapply(1:10, fun)
>
>From: Agustin Lobo <alobo at ija.csic.es>
>To: r-help <r-help at stat.math.ethz.ch>
>MIME-Version: 1.0
>X-Virus-Scanned: by amavisd-milter (http://amavis.org/)
>X-Virus-Scanned: by amavisd-milter (http://amavis.org/)
>X-Spam-Status: No, hits=-0.1 required=5.0
tests=SPAM_PHRASE_00_01,TO_LOCALPART_EQ_REAL,USER_AGENT_PINE
version=2.43>X-Spam-Level:
>Subject: [R] Advice on long for loop
>X-BeenThere: r-help at stat.math.ethz.ch
>X-Mailman-Version: 2.0.13
>List-Help: <mailto:r-help-request at stat.math.ethz.ch?subject=help>
>List-Post: <mailto:r-help at stat.math.ethz.ch>
>List-Subscribe:
<http://www.stat.math.ethz.ch/mailman/listinfo/r-help>,
<mailto:r-help-request at
stat.math.ethz.ch?subject=subscribe>>List-Id: Main R Mailing List: Primary help <r-help.stat.math.ethz.ch>
>List-Unsubscribe:
<http://www.stat.math.ethz.ch/mailman/listinfo/r-help>,
<mailto:r-help-request at
stat.math.ethz.ch?subject=unsubscribe>>List-Archive: <http://www.stat.math.ethz.ch/pipermail/r-help/>
>X-Original-Date: Fri, 13 Dec 2002 15:22:22 +0100 (MET)
>Date: Fri, 13 Dec 2002 15:22:22 +0100 (MET)
>
>
>I'd appreciate advice on the following:
>
>I've written an R function
>that uses 3 vectors (temperature,
>precipitation and potential evapotranspiration)
>for a given site, calculates a water budget
>(which implies few (<4) iterations), and, from
>thresults of this water budget, calculates a number of
>bioclimatic indices.
>
>Now I want to calculate these indices
>for a large number of sites (arranged
>as cells in a raster map), using
>monthly raster maps (m rows x n cols) of temperature,
>precipitation and potential evapotranspiration.
>
>I do not see any other way than using
>two nested for loops and calling the
>aforementioned function for each cell in the
>map. But this would be a problem in R
>as m and n can be ~ 500.
>
>My question is: would it be any better if
>I write a simple python function with the loops
>and call the R function for each cell (using
>the R-Python interface)? Or, as the calculation
>in each cell is independent, should I rather write a
>shell script with the loop and submit jobs
>with R CMD BATCH?
>
>Thanks
>
>Agus
>
>Dr. Agustin Lobo
>Instituto de Ciencias de la Tierra (CSIC)
>Lluis Sole Sabaris s/n
>08028 Barcelona SPAIN
>tel 34 93409 5410
>fax 34 93411 0012
>alobo at ija.csic.es
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>http://www.stat.math.ethz.ch/mailman/listinfo/r-help