[R] how to break the loop using sapply?

Hervé Pagès hpages at fhcrc.org
Fri Oct 10 08:44:47 CEST 2014


Hi,

On 10/09/2014 11:12 PM, PO SU wrote:
>
>
> Is that mean while may be more effient than  for in R? as i know, while and for  are all just functions in R.
> Tks for your suggestion to not use apply that way, but i want to know, if possible, is there any way to break it ?

As Jeff said, you cannot break the loop that happens inside
the sapply() call. Also it is *not* true that for or while are
less efficient than sapply() or lapply():

 > a <- numeric(100000)

 > system.time(for (i in 1:100000) {a[i] <- i * (i - 1) / 2})
    user  system elapsed
   0.148   0.000   0.147

 > system.time(b <- sapply(1:100000, function(i) {i * (i - 1) / 2}))
    user  system elapsed
   0.194   0.007   0.201

 > identical(a, b)
[1] TRUE

 > system.time(c <- unlist(lapply(1:100000, function(i) {i * (i - 1) / 2})))
    user  system elapsed
   0.116   0.000   0.119

 > identical(a, c)
[1] TRUE

OK lapply() is maybe slightly faster but not significantly. And the
more work you need to do inside the loop, the less significant this
difference will be.

> Actually, there is a additional question:
>    x<- c(3,4,5,6,9)
>   sapply(x ,function(i){
> foo(i)  #do something to each value in x,how can i know the i's index in x?
> )}

You can't. Inside the anonymous function, you only have access to 'i'
which is an element of 'x', not its index in 'x'.

> In my way , i always
> sapply(seq(x),function(i){
> foo(x[i])
> })

Yes, if you want to loop on the index instead of the elements, you
need to do something like that. Using seq_along(x) is probably
cleaner than seq(x) for this.

Cheers,
H.

> or
> Map( function(i,index){
> foo(i)  # through index to know the i's index in x
> },x ,seq(x))
>
> How you solve the problem? I mean just use apply functions.
>
>
>
> --
>
> PO SU
> mail: desolator88 at 163.com
> Majored in Statistics from SJTU
>
>
>
>
> At 2014-10-10 13:58:29, "Jeff Newmiller" <jdnewmil at dcn.davis.CA.us> wrote:
>> Don't use apply functions if you want to do what you describe. They don't work that way. Use a while control structure.
>>
>> ---------------------------------------------------------------------------
>> Jeff Newmiller                        The     .....       .....  Go Live...
>> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
>>                                       Live:   OO#.. Dead: OO#..  Playing
>> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
>> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
>> ---------------------------------------------------------------------------
>> Sent from my phone. Please excuse my brevity.
>>
>> On October 9, 2014 10:24:49 PM PDT, PO SU <rhelpmaillist at 163.com> wrote:
>>>
>>> Dear expeRts,
>>>     i  use sapply for loop, and i want to break it when i needed, how to
>>> do that?  e.g.
>>>
>>> sapply( 1:10, function(i) {
>>> if(i==5) break and jump out of the function sapply
>>> } )
>>>
>>> I want to do it because i have to loop 1000000 times, but i don't know
>>> when it will break, that means, it may need break at i=5 or at i=50000,
>>> for the possible of the  last case, i don't use for loop, because it
>>> slow(is it right?).
>>> So,if you happen to know it ,may you help me?
>>>
>>>
>>> --
>>>
>>> PO SU
>>> mail: desolator88 at 163.com
>>> Majored in Statistics from SJTU
>>> ______________________________________________
>>> 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.
>

-- 
Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpages at fhcrc.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319



More information about the R-help mailing list