Incrementing a counter in lapply

classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Incrementing a counter in lapply

John McHenry
    Hi All,

    I'm looking for some hints on idiomatic R usage using 'lapply' or similar.
    What follows is a simple example from which to generalize my question...

    # Suppose, in this simple example, I want to plot a number of different lines in different colors;
    # I define the colors I wish to use and I plot them in a loop:
    d<- data.frame(read.table(textConnection("
         Y     X     D
                85    30     0
                95    40     1
                90    40     1
                75    20     0
           100    60     1
                90    40     0
                90    50     0
                90    30     1
               100    60     1
                85    30     1"
    ), header=TRUE))
    # graph the relation of Y to X when
    #     i)  D==0
    #     ii) D==1
    with( d, plot(X, Y, type="n") )
    component<- with( d, split(d, D) )
    colors<- c("blue", "green")
    for (i in 1:length(component))
        with( component[[i]], lines(X, predict(lm(Y ~ X)), col=colors[i]) )

    #
    # ... seems easy enough
    #
    # [Q.]: How to do the same as the above but using 'lapply'?
    # ... i.e. something along the lines of:
    with( d, plot(X, Y, type="n") )
    colors<- c("blue", "green")
    # how do I get lapply to increment i?
    lapply( with(d, split(d, D)), function(z) with(z, lines(X, predict(lm(Y ~ X)), col=colors[i])) )
         
Thanks,

Jack.


                       
---------------------------------


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Incrementing a counter in lapply

Thomas Lumley
On Mon, 13 Mar 2006, John McHenry wrote:

>    Hi All,
>
>    I'm looking for some hints on idiomatic R usage using 'lapply' or similar.
>    What follows is a simple example from which to generalize my question...
>
>    # Suppose, in this simple example, I want to plot a number of different lines in different colors;
>    # I define the colors I wish to use and I plot them in a loop:
>    d<- data.frame(read.table(textConnection("
>         Y     X     D
>                85    30     0
>                95    40     1
>                90    40     1
>                75    20     0
>           100    60     1
>                90    40     0
>                90    50     0
>                90    30     1
>               100    60     1
>                85    30     1"
>    ), header=TRUE))
>    # graph the relation of Y to X when
>    #     i)  D==0
>    #     ii) D==1
>    with( d, plot(X, Y, type="n") )
>    component<- with( d, split(d, D) )
>    colors<- c("blue", "green")
>    for (i in 1:length(component))
>        with( component[[i]], lines(X, predict(lm(Y ~ X)), col=colors[i]) )
>
>    #
>    # ... seems easy enough
>    #
>    # [Q.]: How to do the same as the above but using 'lapply'?
>    # ... i.e. something along the lines of:
>    with( d, plot(X, Y, type="n") )
>    colors<- c("blue", "green")
>    # how do I get lapply to increment i?
>    lapply( with(d, split(d, D)), function(z) with(z, lines(X, predict(lm(Y ~ X)), col=colors[i])) )
>

You can't get lapply to increment i, but you can use mapply and write your
function with two arguments.

mapply( function(z,colour) with(z, lines(X, predict(lm(Y~X), col=colour)),
         with(d, split(d,D)),
         colors)



  -thomas

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Incrementing a counter in lapply

Gabor Grothendieck
In reply to this post by John McHenry
Try this:

plot(Y ~ X, d, type = "n")
f <- function(i) abline(lm(Y ~ X, d, subset = D == i), col = colors[i+1])
junk <- lapply(unique(d$D), f)




On 3/13/06, John McHenry <[hidden email]> wrote:

>    Hi All,
>
>    I'm looking for some hints on idiomatic R usage using 'lapply' or similar.
>    What follows is a simple example from which to generalize my question...
>
>    # Suppose, in this simple example, I want to plot a number of different lines in different colors;
>    # I define the colors I wish to use and I plot them in a loop:
>    d<- data.frame(read.table(textConnection("
>         Y     X     D
>                85    30     0
>                95    40     1
>                90    40     1
>                75    20     0
>           100    60     1
>                90    40     0
>                90    50     0
>                90    30     1
>               100    60     1
>                85    30     1"
>    ), header=TRUE))
>    # graph the relation of Y to X when
>    #     i)  D==0
>    #     ii) D==1
>    with( d, plot(X, Y, type="n") )
>    component<- with( d, split(d, D) )
>    colors<- c("blue", "green")
>    for (i in 1:length(component))
>        with( component[[i]], lines(X, predict(lm(Y ~ X)), col=colors[i]) )
>
>    #
>    # ... seems easy enough
>    #
>    # [Q.]: How to do the same as the above but using 'lapply'?
>    # ... i.e. something along the lines of:
>    with( d, plot(X, Y, type="n") )
>    colors<- c("blue", "green")
>    # how do I get lapply to increment i?
>    lapply( with(d, split(d, D)), function(z) with(z, lines(X, predict(lm(Y ~ X)), col=colors[i])) )
>
> Thanks,
>
> Jack.
>
>
>
> ---------------------------------
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Incrementing a counter in lapply

John McHenry
Thanks, Gabor & Thomas.

Apologies, but I used an example that obfuscated the question that I wanted to ask.

I really wanted to know how to have extra arguments in functions that would allow, per the example code, for something like a counter to be incremented.
Thomas's suggestion of using mapply (reproduced below with corrections) is probably closest.

Jack.

PS Here's the corrected code:

    d<- data.frame(read.table(textConnection("
         Y     X     D
                85    30     0
                95    40     1
                90    40     1
                75    20     0
           100    60     1
                90    40     0
                90    50     0
                90    30     1
               100    60     1
                85    30     1"
    ), header=TRUE))
    windows(); plot(Y ~ X, d, type="n")
    colors<- c("blue","green")
    junk<- mapply(
        function(z,color) with(z, lines(X, predict(lm(Y~X)), col=color)),
        with(d, split(d,D)),
        color=colors
    )




Thomas Lumley <[hidden email]> wrote:
You can't get lapply to increment i, but you can use mapply and write your
function with two arguments.

mapply( function(z,colour) with(z, lines(X, predict(lm(Y~X), col=colour)),
         with(d, split(d,D)),
         colors)



  -thomas


Gabor Grothendieck <[hidden email]> wrote: Try this:

plot(Y ~ X, d, type = "n")
f <- function(i) abline(lm(Y ~ X, d, subset = D == i), col = colors[i+1])
junk <- lapply(unique(d$D), f)




On 3/13/06, John McHenry  wrote:

>    Hi All,
>
>    I'm looking for some hints on idiomatic R usage using 'lapply' or similar.
>    What follows is a simple example from which to generalize my question...
>
>    # Suppose, in this simple example, I want to plot a number of different lines in different colors;
>    # I define the colors I wish to use and I plot them in a loop:
>    d<- data.frame(read.table(textConnection("
>         Y     X     D
>                85    30     0
>                95    40     1
>                90    40     1
>                75    20     0
>           100    60     1
>                90    40     0
>                90    50     0
>                90    30     1
>               100    60     1
>                85    30     1"
>    ), header=TRUE))
>    # graph the relation of Y to X when
>    #     i)  D==0
>    #     ii) D==1
>    with( d, plot(X, Y, type="n") )
>    component<- with( d, split(d, D) )
>    colors<- c("blue", "green")
>    for (i in 1:length(component))
>        with( component[[i]], lines(X, predict(lm(Y ~ X)), col=colors[i]) )
>
>    #
>    # ... seems easy enough
>    #
>    # [Q.]: How to do the same as the above but using 'lapply'?
>    # ... i.e. something along the lines of:
>    with( d, plot(X, Y, type="n") )
>    colors<- c("blue", "green")
>    # how do I get lapply to increment i?
>    lapply( with(d, split(d, D)), function(z) with(z, lines(X, predict(lm(Y ~ X)), col=colors[i])) )
>
> Thanks,
>
> Jack.
>
>
>
> ---------------------------------
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>


               
---------------------------------

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Incrementing a counter in lapply

Gabor Grothendieck
There are two ways to do that: one is to use mapply as Thomas
showed and the other is to iterate over an index rather than over
the data itself.  Here is a rather lame example using + but hopefully
it conveys the idea of the two possibilities:

# 1
x <- y <- 1:4
mapply("+", x, y)

# 2
sapply(seq(along = x), function(i) x[i]+y[i])

On 3/14/06, John McHenry <[hidden email]> wrote:

> Thanks, Gabor & Thomas.
>
> Apologies, but I used an example that obfuscated the question that I wanted
> to ask.
>
> I really wanted to know how to have extra arguments in functions that would
> allow, per the example code, for something like a counter to be incremented.
> Thomas's suggestion of using mapply (reproduced below with corrections) is
> probably closest.
>
> Jack.
>
> PS Here's the corrected code:
>
>
>     d<- data.frame(read.table(textConnection("
>          Y     X     D
>                 85    30     0
>                 95    40     1
>                 90    40     1
>                 75    20     0
>            100    60     1
>                 90    40     0
>                 90    50     0
>                 90    30     1
>                100    60     1
>                 85    30     1"
>     ), header=TRUE))
>     windows(); plot(Y ~ X, d, type="n")
>     colors<- c("blue","green")
>     junk<- mapply(
>         function(z,color) with(z, lines(X, predict(lm(Y~X)), col=color)),
>         with(d, split(d,D)),
>         color=colors
>     )
>
>
>
>
>
> Thomas Lumley <[hidden email]> wrote:
>
> You can't get lapply to increment i, but you can use mapply and write your
> function with two arguments.
>
> mapply( function(z,colour) with(z, lines(X, predict(lm(Y~X), col=colour)),
>
> with(d, split(d,D)),
> colors)
>
>
>
> -thomas
>
>
>
> Gabor Grothendieck <[hidden email]> wrote:
>
> Try this:
>
> plot(Y ~ X, d, type = "n")
> f <- function(i) abline(lm(Y ~ X, d, subset = D == i), col = colors[i+1])
> junk <- lapply(unique(d$D), f)
>
>
>
>
> On 3/13/06, John McHenry wrote:
> > Hi All,
> >
> > I'm looking for some hints on idiomatic R usage using 'lapply' or similar.
> > What follows is a simple example from which to generalize my question...
> >
> > # Suppose, in this simple example, I want to plot a number of different
> lines in different colors;
> > # I define the colors I wish to use and I plot them in a loop:
> > d<- data.frame(read.table(textConnection("
> > Y X D
> > 85 30 0
> > 95 40 1
> > 90 40 1
> > 75 20 0
> > 100 60 1
> > 90 40 0
> > 90 50 0
> > 90 30 1
> > 100 60 1
> > 85 30 1"
> > ), header=TRUE))
> > # graph the relation of Y to X when
> > # i) D==0
> > # ii) D==1
> > with( d, plot(X, Y, type="n") )
> > component<- with( d, split(d, D) )
> > colors<- c("blue", "green")
> > for (i in 1:length(component))
> > with( component[[i]], lines(X, predict(lm(Y ~ X)), col=colors[i]) )
> >
> > #
> > # ... seems easy enough
> > #
> > # [Q.]: How to do the same as the above but using 'lapply'?
> > # ... i.e. something along the lines of:
> > with( d, plot(X, Y, type="n") )
> > colors<- c("blue", "green")
> > # how do I get lapply to increment i?
> > lapply( with(d, split(d, D)), function(z) with(z, lines(X, predict(lm(Y ~
> X)), col=colors[i])) )
> >
> > Thanks,
> >
> > Jack.
> >
> >
> >
> > ---------------------------------
> >
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
> >
>
>
>
>
> ________________________________
> Relax. Yahoo! Mail virus scanning helps detect nasty viruses!
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Incrementing a counter in lapply

Thomas Lumley
In reply to this post by John McHenry
On Tue, 14 Mar 2006, John McHenry wrote:

> Thanks, Gabor & Thomas.
>
> Apologies, but I used an example that obfuscated the question that I
> wanted to ask.
>
> I really wanted to know how to have extra arguments in functions that
> would allow, per the example code, for something like a counter to be
> incremented. Thomas's suggestion of using mapply (reproduced below with
> corrections) is probably closest.

It is probably worth pointing out here that the R documentation does not
specify the order in which lapply() does the computation.

If you could work out how to increment a counter (and you could, with
sufficient effort), it would not necessarily work, because the 'i'th
evaluation would not necessarily be of the 'i'th element.

[lapply() does in fact start at the beginning, go on until it gets to the
end, and then stop, but this isn't documented.   Suppose R became
multithreaded, for example....]

  -thomas


>
> Jack.
>
> PS Here's the corrected code:
>
>    d<- data.frame(read.table(textConnection("
>         Y     X     D
>                85    30     0
>                95    40     1
>                90    40     1
>                75    20     0
>           100    60     1
>                90    40     0
>                90    50     0
>                90    30     1
>               100    60     1
>                85    30     1"
>    ), header=TRUE))
>    windows(); plot(Y ~ X, d, type="n")
>    colors<- c("blue","green")
>    junk<- mapply(
>        function(z,color) with(z, lines(X, predict(lm(Y~X)), col=color)),
>        with(d, split(d,D)),
>        color=colors
>    )
>
>
>
>
> Thomas Lumley <[hidden email]> wrote:
> You can't get lapply to increment i, but you can use mapply and write your
> function with two arguments.
>
> mapply( function(z,colour) with(z, lines(X, predict(lm(Y~X), col=colour)),
>         with(d, split(d,D)),
>         colors)
>
>
>
>  -thomas
>
>
> Gabor Grothendieck <[hidden email]> wrote: Try this:
>
> plot(Y ~ X, d, type = "n")
> f <- function(i) abline(lm(Y ~ X, d, subset = D == i), col = colors[i+1])
> junk <- lapply(unique(d$D), f)
>
>
>
>
> On 3/13/06, John McHenry  wrote:
>>    Hi All,
>>
>>    I'm looking for some hints on idiomatic R usage using 'lapply' or similar.
>>    What follows is a simple example from which to generalize my question...
>>
>>    # Suppose, in this simple example, I want to plot a number of different lines in different colors;
>>    # I define the colors I wish to use and I plot them in a loop:
>>    d<- data.frame(read.table(textConnection("
>>         Y     X     D
>>                85    30     0
>>                95    40     1
>>                90    40     1
>>                75    20     0
>>           100    60     1
>>                90    40     0
>>                90    50     0
>>                90    30     1
>>               100    60     1
>>                85    30     1"
>>    ), header=TRUE))
>>    # graph the relation of Y to X when
>>    #     i)  D==0
>>    #     ii) D==1
>>    with( d, plot(X, Y, type="n") )
>>    component<- with( d, split(d, D) )
>>    colors<- c("blue", "green")
>>    for (i in 1:length(component))
>>        with( component[[i]], lines(X, predict(lm(Y ~ X)), col=colors[i]) )
>>
>>    #
>>    # ... seems easy enough
>>    #
>>    # [Q.]: How to do the same as the above but using 'lapply'?
>>    # ... i.e. something along the lines of:
>>    with( d, plot(X, Y, type="n") )
>>    colors<- c("blue", "green")
>>    # how do I get lapply to increment i?
>>    lapply( with(d, split(d, D)), function(z) with(z, lines(X, predict(lm(Y ~ X)), col=colors[i])) )
>>
>> Thanks,
>>
>> Jack.
>>
>>
>>
>> ---------------------------------
>>
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>>
>
>
>
> ---------------------------------
> Relax. Yahoo! Mail virus scanning helps detect nasty viruses!

Thomas Lumley Assoc. Professor, Biostatistics
[hidden email] University of Washington, Seattle

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Incrementing a counter in lapply

Liaw, Andy
In reply to this post by John McHenry
From: Thomas Lumley

>
> On Tue, 14 Mar 2006, John McHenry wrote:
>
> > Thanks, Gabor & Thomas.
> >
> > Apologies, but I used an example that obfuscated the question that I
> > wanted to ask.
> >
> > I really wanted to know how to have extra arguments in
> functions that
> > would allow, per the example code, for something like a
> counter to be
> > incremented. Thomas's suggestion of using mapply
> (reproduced below with
> > corrections) is probably closest.
>
> It is probably worth pointing out here that the R
> documentation does not
> specify the order in which lapply() does the computation.
>
> If you could work out how to increment a counter (and you could, with
> sufficient effort), it would not necessarily work, because the 'i'th
> evaluation would not necessarily be of the 'i'th element.
>
> [lapply() does in fact start at the beginning, go on until it
> gets to the
> end, and then stop, but this isn't documented.   Suppose R became
> multithreaded, for example....]

The corollary, it seems to me, is that sometimes it's better to leave the
good old for loop alone.  It's not always profitable to turn for loops into
some *apply construct.  The trick is learning to know when to do it and when
not to.

Andy

 

>   -thomas
>
>
> >
> > Jack.
> >
> > PS Here's the corrected code:
> >
> >    d<- data.frame(read.table(textConnection("
> >         Y     X     D
> >                85    30     0
> >                95    40     1
> >                90    40     1
> >                75    20     0
> >           100    60     1
> >                90    40     0
> >                90    50     0
> >                90    30     1
> >               100    60     1
> >                85    30     1"
> >    ), header=TRUE))
> >    windows(); plot(Y ~ X, d, type="n")
> >    colors<- c("blue","green")
> >    junk<- mapply(
> >        function(z,color) with(z, lines(X, predict(lm(Y~X)),
> col=color)),
> >        with(d, split(d,D)),
> >        color=colors
> >    )
> >
> >
> >
> >
> > Thomas Lumley <[hidden email]> wrote:
> > You can't get lapply to increment i, but you can use mapply
> and write
> > your function with two arguments.
> >
> > mapply( function(z,colour) with(z, lines(X,
> predict(lm(Y~X), col=colour)),
> >         with(d, split(d,D)),
> >         colors)
> >
> >
> >
> >  -thomas
> >
> >
> > Gabor Grothendieck <[hidden email]> wrote: Try this:
> >
> > plot(Y ~ X, d, type = "n")
> > f <- function(i) abline(lm(Y ~ X, d, subset = D == i), col =
> > colors[i+1]) junk <- lapply(unique(d$D), f)
> >
> >
> >
> >
> > On 3/13/06, John McHenry  wrote:
> >>    Hi All,
> >>
> >>    I'm looking for some hints on idiomatic R usage using
> 'lapply' or similar.
> >>    What follows is a simple example from which to generalize my
> >> question...
> >>
> >>    # Suppose, in this simple example, I want to plot a
> number of different lines in different colors;
> >>    # I define the colors I wish to use and I plot them in a loop:
> >>    d<- data.frame(read.table(textConnection("
> >>         Y     X     D
> >>                85    30     0
> >>                95    40     1
> >>                90    40     1
> >>                75    20     0
> >>           100    60     1
> >>                90    40     0
> >>                90    50     0
> >>                90    30     1
> >>               100    60     1
> >>                85    30     1"
> >>    ), header=TRUE))
> >>    # graph the relation of Y to X when
> >>    #     i)  D==0
> >>    #     ii) D==1
> >>    with( d, plot(X, Y, type="n") )
> >>    component<- with( d, split(d, D) )
> >>    colors<- c("blue", "green")
> >>    for (i in 1:length(component))
> >>        with( component[[i]], lines(X, predict(lm(Y ~ X)),
> >> col=colors[i]) )
> >>
> >>    #
> >>    # ... seems easy enough
> >>    #
> >>    # [Q.]: How to do the same as the above but using 'lapply'?
> >>    # ... i.e. something along the lines of:
> >>    with( d, plot(X, Y, type="n") )
> >>    colors<- c("blue", "green")
> >>    # how do I get lapply to increment i?
> >>    lapply( with(d, split(d, D)), function(z) with(z, lines(X,
> >> predict(lm(Y ~ X)), col=colors[i])) )
> >>
> >> Thanks,
> >>
> >> Jack.
> >>
> >>
> >>
> >> ---------------------------------
> >>
> >>
> >>        [[alternative HTML version deleted]]
> >>
> >> ______________________________________________
> >> [hidden email] mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide!
> >> http://www.R-project.org/posting-guide.html
> >>
> >
> >
> >
> > ---------------------------------
> > Relax. Yahoo! Mail virus scanning helps detect nasty viruses!
>
> Thomas Lumley Assoc. Professor, Biostatistics
> [hidden email] University of Washington, Seattle
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Incrementing a counter in lapply

Gabor Grothendieck
In reply to this post by Thomas Lumley
On 3/14/06, Thomas Lumley <[hidden email]> wrote:

> On Tue, 14 Mar 2006, John McHenry wrote:
>
> > Thanks, Gabor & Thomas.
> >
> > Apologies, but I used an example that obfuscated the question that I
> > wanted to ask.
> >
> > I really wanted to know how to have extra arguments in functions that
> > would allow, per the example code, for something like a counter to be
> > incremented. Thomas's suggestion of using mapply (reproduced below with
> > corrections) is probably closest.
>
> It is probably worth pointing out here that the R documentation does not
> specify the order in which lapply() does the computation.
>

I suspect that a huge amount of application code takes advantage
of this order.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Incrementing a counter in lapply

Thomas Lumley
On Tue, 14 Mar 2006, Gabor Grothendieck wrote:

> On 3/14/06, Thomas Lumley <[hidden email]> wrote:
>> It is probably worth pointing out here that the R documentation does not
>> specify the order in which lapply() does the computation.
>>
>
> I suspect that a huge amount of application code takes advantage
> of this order.
>

I don't.  The order of evaluation is usually not readily observable. You
either have to use <<- to modify an external variable or you have to
produce printed or graphical output where the order matters.

There's probably some examples, but there are some examples of people
using solve(t(X) %*% W %*% X) %*% W %*% Y to compute regression
coefficients, too.


  -thomas

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Incrementing a counter in lapply

Gabor Grothendieck
On 3/14/06, Thomas Lumley <[hidden email]> wrote:

> On Tue, 14 Mar 2006, Gabor Grothendieck wrote:
>
> > On 3/14/06, Thomas Lumley <[hidden email]> wrote:
> >> It is probably worth pointing out here that the R documentation does not
> >> specify the order in which lapply() does the computation.
> >>
> >
> > I suspect that a huge amount of application code takes advantage
> > of this order.
> >
>
> I don't.  The order of evaluation is usually not readily observable. You
> either have to use <<- to modify an external variable or you have to
> produce printed or graphical output where the order matters.
>
> There's probably some examples, but there are some examples of people
> using solve(t(X) %*% W %*% X) %*% W %*% Y to compute regression
> coefficients, too.

By order do you mean that the result is returned in a random order
or that the result is returned in a fixed order but computed it a random
order?

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Incrementing a counter in lapply

Thomas Lumley
On Tue, 14 Mar 2006, Gabor Grothendieck wrote:

> On 3/14/06, Thomas Lumley <[hidden email]> wrote:
>> On Tue, 14 Mar 2006, Gabor Grothendieck wrote:
>>
>>> On 3/14/06, Thomas Lumley <[hidden email]> wrote:
>>>> It is probably worth pointing out here that the R documentation does not
>>>> specify the order in which lapply() does the computation.
>>>>
>>>
>>> I suspect that a huge amount of application code takes advantage
>>> of this order.
>>>
>>
>> I don't.  The order of evaluation is usually not readily observable. You
>> either have to use <<- to modify an external variable or you have to
>> produce printed or graphical output where the order matters.
>>
>> There's probably some examples, but there are some examples of people
>> using solve(t(X) %*% W %*% X) %*% W %*% Y to compute regression
>> coefficients, too.
>
> By order do you mean that the result is returned in a random order
> or that the result is returned in a fixed order but computed it a random
> order?
>

It is returned in a fixed order, the first output corresponding to the
first input.  The documentation does not specify what order it is
*computed* in.

This in contrast to eapply(), which returns the results in a random order.

  -thomas

Thomas Lumley Assoc. Professor, Biostatistics
[hidden email] University of Washington, Seattle

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Incrementing a counter in lapply

Gabor Grothendieck
On 3/14/06, Thomas Lumley <[hidden email]> wrote:

> On Tue, 14 Mar 2006, Gabor Grothendieck wrote:
>
> > On 3/14/06, Thomas Lumley <[hidden email]> wrote:
> >> On Tue, 14 Mar 2006, Gabor Grothendieck wrote:
> >>
> >>> On 3/14/06, Thomas Lumley <[hidden email]> wrote:
> >>>> It is probably worth pointing out here that the R documentation does not
> >>>> specify the order in which lapply() does the computation.
> >>>>
> >>>
> >>> I suspect that a huge amount of application code takes advantage
> >>> of this order.
> >>>
> >>
> >> I don't.  The order of evaluation is usually not readily observable. You
> >> either have to use <<- to modify an external variable or you have to
> >> produce printed or graphical output where the order matters.
> >>
> >> There's probably some examples, but there are some examples of people
> >> using solve(t(X) %*% W %*% X) %*% W %*% Y to compute regression
> >> coefficients, too.
> >
> > By order do you mean that the result is returned in a random order
> > or that the result is returned in a fixed order but computed it a random
> > order?
> >
>
> It is returned in a fixed order, the first output corresponding to the
> first input.  The documentation does not specify what order it is
> *computed* in.
>
> This in contrast to eapply(), which returns the results in a random order.

In that case I misunderstood.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html