Re: [R] 'for' loop, two variables

From: ONKELINX, Thierry <Thierry.ONKELINX_at_inbo.be>
Date: Tue, 29 Jul 2008 14:13:02 +0200

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_at_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_at_r-project.org [mailto:r-help-bounces_at_r-project.org] Namens Oehler, Friderike (AGPP)
Verzonden: dinsdag 29 juli 2008 13:56
Aan: Oehler, Friderike (AGPP); r-help_at_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_at_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.

R-help_at_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. Received on Tue 29 Jul 2008 - 12:16:25 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 29 Jul 2008 - 13:32:46 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive