[R] help with loops

William Dunlap wdunlap at tibco.com
Thu Aug 11 21:51:10 CEST 2011


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of R. Michael
> Weylandt
> Sent: Thursday, August 11, 2011 10:09 AM
> To: Srinivas Iyyer
> Cc: r-help at r-project.org
> Subject: Re: [R] help with loops
> 
> No problem,
> 
> By the way, you can't (or at least shouldn't) use return() outside of a
> function -- that was the source of your old error message.
> 
> If you, for whatever reason, couldn't use unlist() you would write:
> 
> OurUnlist <- function(c, unique = F) {
>       if (!is.list(c)) return(c)
>       z <- NULL
>       for (i in seq_along(c)) {
>            z <- c(z,c[[i]])
>       }
>       if (unique) return(unique(z))
>       return(z)
> }
> 
> or some such. Still, I suggest you stick with built in functions whenever
> possible.

I tend to encourage people to write functions.

I suppose you may end up reinventing the wheel,
but once you get used to writing functions it
is often faster to write a specialized one than
to find one that meets your needs.  When you
discover a new idiom for your task (e.g., calling
unlist() instead of the for loop), you just edit
one function (OurUnlist) instead of editing all
your scripts that used the old idiom).

Once you get used to writing functions (and using
them), you are ready to document them and package
them up for others to use.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> 
> Michael Weylandt
> 
> PS -- Can you email me (off list) and let me know what this is for? We've
> been asked this question a couple of times over the last few days and I'm
> just wondering why it's a calculation of interest to so many.
> 
> On Thu, Aug 11, 2011 at 1:00 PM, Srinivas Iyyer
> <srini_iyyer_bio at yahoo.com>wrote:
> 
> > Thank you. that was very easy.
> > -srini
> >
> > --- On *Thu, 8/11/11, R. Michael Weylandt <michael.weylandt at gmail.com>*wrote:
> >
> >
> > From: R. Michael Weylandt <michael.weylandt at gmail.com>
> > Subject: Re: [R] help with loops
> > To: "Srinivas Iyyer" <srini_iyyer_bio at yahoo.com>
> > Cc: r-help at r-project.org
> > Date: Thursday, August 11, 2011, 12:49 PM
> >
> >
> > unlist()
> >
> > Michael Weylandt
> >
> > On Thu, Aug 11, 2011 at 12:46 PM, Srinivas Iyyer <
> > srini_iyyer_bio at yahoo.com <http://mc/compose?to=srini_iyyer_bio@yahoo.com>
> > > wrote:
> >
> > hi I need help with list object.
> >
> > I have a list object
> >
> > > a <- c('apple','orange','grape')
> > > b <- c('car','truck','jeep')
> > > c <- list(a,b)
> > > names(c) <- c('fruit','vehicle')
> > > c
> > $fruit
> > [1] "apple"  "orange" "grape"
> >
> > $vehicle
> > [1] "car"   "truck" "jeep"
> >
> >
> > I want to write all the elements of this list in one object 'z'.
> >
> > >z
> > [1] "apple"  "orange" "grape" "car"   "truck" "jeep"
> >
> > How can I write the elements of c to z
> > I tried using a for loop. Could any one help me please. thanks
> >
> >
> > > z <- ''
> > > for (i in 1:length(c)){
> > + k <- c[[i]]
> > + z <- c(z,k)
> > + return(z)}
> > Error: no function to return from, jumping to top level
> > >
> >
> >
> > Thank you.
> > srini
> >
> > ______________________________________________
> > R-help at r-project.org <http://mc/compose?to=R-help@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.
> >
> >
> >
> 
> 	[[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.



More information about the R-help mailing list