Re: [R] Repeat if

From: Christophe Pallier <christophe_at_pallier.org>
Date: Fri, 29 Jun 2007 14:33:37 +0200

On 6/29/07, Birgit Lemcke <birgit.lemcke_at_systbot.uzh.ch> wrote:
>
> Hello Jim,
>
> thanks for your answer. At the moment I am using this code:
>
> Range0<-sapply(1:85, function(i) eval(parse(text=paste("range(V", i,
> ", na.rm=T)", sep=""))))

It is a matter of taste, but I tend to prefer:

a <- list()
for (i in 1:85) a[[i]] = get(paste("V", i, spe=""))

sapply(a, range, na.rm=T)

The first two lines put your variables inside a list (assuming they are not already in one, e.g., in a data.frame).
This is especially interesting if you have other operations to perform on the V1..V85 variables.

Christophe

and it works really fine.
>
> The code you sent me is also fine but how can I implement, that
> missing values are TRUE?
>
> Thanks a lot for your help
>
> Birgit
>
> Am 28.06.2007 um 13:01 schrieb Jim Lemon:
>
> > Birgit Lemcke wrote:
> >> Hello,
> >> (Power Book G4, Mac OS X, R 2.5.0)
> >> I would like to repeat the function range for 85 Vectors (V1-V85).
> >> I tried with this code:
> >> i<-0
> >> > repeat {
> >> + i<-i+1
> >> + if (i<85) next
> >> + range (Vi, na.rm = TRUE)
> >> + if (i==85) break
> >> + }
> >> I presume that the Vi is wrong, because in this syntax i is not
> >> known as a variable. But I don´t know how to say that it is a
> >> variable here.
> >> Would be nice if somebody could help me.
> >> Perhaps I´m thinking too complicated and there is an easier way to
> >> do this.
> > Hi Birgit,
> > This may be what you want:
> >
> > for(i in 1:85)
> > print(do.call("range",list(as.name(paste("V",i,sep="")))))
> >
> > Jim
> >
>
> Birgit Lemcke
> Institut für Systematische Botanik
> Zollikerstrasse 107
> CH-8008 Zürich
> Switzerland
> Ph: +41 (0)44 634 8351
> birgit.lemcke_at_systbot.uzh.ch
>
>
>
>
>
>
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help_at_stat.math.ethz.ch 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.
>
>

-- 
Christophe Pallier (http://www.pallier.org)

	[[alternative HTML version deleted]]


______________________________________________ R-help_at_stat.math.ethz.ch 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 Fri 29 Jun 2007 - 12:39:46 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 Fri 29 Jun 2007 - 13:32:37 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.