> -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf Of R. Michael > Weylandt > Sent: Thursday, August 11, 2011 10:09 AM > To: Srinivas Iyyer > Cc: r-help@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_...@yahoo.com>wrote: > > > Thank you. that was very easy. > > -srini > > > > --- On *Thu, 8/11/11, R. Michael Weylandt > > <michael.weyla...@gmail.com>*wrote: > > > > > > From: R. Michael Weylandt <michael.weyla...@gmail.com> > > Subject: Re: [R] help with loops > > To: "Srinivas Iyyer" <srini_iyyer_...@yahoo.com> > > Cc: r-help@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_...@yahoo.com <http://mc/compose?to=srini_iyyer_...@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@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@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@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.