[R] help with loops

Paul Hiemstra paul.hiemstra at knmi.nl
Fri Aug 12 09:22:41 CEST 2011


 On 08/11/2011 07:51 PM, William Dunlap wrote:
>> -----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.

In addition, writing functions yourself is a good way to exercise your R
skills. On the other hand, built in functions often solve the problem
faster and are more generic. In my experience it takes quite a bit of R
knowledge before one is good enough to beat a general, builtin function.
Often a lengthy self written function can be replaced by one call to a
built in function. In addition, it saves a lot of time when you use the
already present functions. Several times I wanted something done in R,
only to find out that it was already done. This meant getting the job
done in 1 hour instead of two days of programming.

In general I tend to agree with Michael and encourage people to stick
with the builtin functions.

my 2 cts ;),

regards,
Paul

> 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.
> ______________________________________________
> 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.


-- 
Paul Hiemstra, Ph.D.
Global Climate Division
Royal Netherlands Meteorological Institute (KNMI)
Wilhelminalaan 10 | 3732 GK | De Bilt | Kamer B 3.39
P.O. Box 201 | 3730 AE | De Bilt
tel: +31 30 2206 494

http://intamap.geo.uu.nl/~paul
http://nl.linkedin.com/pub/paul-hiemstra/20/30b/770



More information about the R-help mailing list