[R] 'for' loop, two variables

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Tue Jul 29 14:13:02 CEST 2008


Dear Frederike,

#Both your functions are vectorized. So you don't need loops. Working
with vectorized functions is much faster than looping.

fn <- function (x) {
    ifelse(x>46 & x<52, 1, 0)
}    
res <- fn(40:60)

fn <- function (x,y) {
    ifelse(x>46 & x<52 & y<12, 1, 0)
}    
datagrid <- expand.grid(i = 40:60, j = 0:20)
res <- fn(datagrid$i, datagrid$j)

#An other option is to use the functions for the apply-family

fn <- function (x) {
    ifelse(x>46 & x<52, 1, 0)
}    
res <- sapply(40:60, fn)

fn <- function (x,y) {
    ifelse(x>46 & x<52 & y<12, 1, 0)
}    
datagrid <- expand.grid(i = 40:60, j = 0:20)
res <- apply(datagrid, 1, function(z){
    fn(z["i"], z["j"])
}) 

#or you can use a nested loop

res <-NULL 
for (i in 40:60){
    for(j in 0:20){
        res <-c(res,fn(i,j))
    }
}

HTH,

Thierry

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium 
tel. + 32 54/436 185
Thierry.Onkelinx op inbo.be 
www.inbo.be 

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher

The plural of anecdote is not data.
~ Roger Brinner

The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey

-----Oorspronkelijk bericht-----
Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org]
Namens Oehler, Friderike (AGPP)
Verzonden: dinsdag 29 juli 2008 13:56
Aan: Oehler, Friderike (AGPP); r-help op r-project.org
Onderwerp: [R] 'for' loop, two variables

Dear Rusers,
I am still an unexperienced builder of functions and loops, so my
question is
very basic: Is it possible to introduce a second variable (j) into my
loop.
To examplify:

# This works fine:
fn <- function (x) {if (x>46 & x<52) 1 else 0}    
res <-NULL 
for (i in 40:60) res <-c(res,fn(i))
res

# But here, there is an error in the "for" expression:
fn <- function (x,y) {if (x>46 & x<52 & y<12) 1 else 0 }    
res <-NULL 
for (i in 40:60 & j in 0:20) res <-c(res,fn(i,j)) 
# How do I have to write the expression "i in 40:60 & j in 0:20"? Or is
there
no way to do that, i.e. I have to do the calculation in two steps?

Thanks in advance!
Friderike

	[[alternative HTML version deleted]]

______________________________________________
R-help op r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list