From maechler at stat.math.ethz.ch  Mon Jun 26 19:04:47 2017
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Mon, 26 Jun 2017 19:04:47 +0200
Subject: [Rd] Odd behaviour in within.list() when deleting 2+ variables
In-Reply-To: <FDD724E2-B293-4B4B-A787-E1BA9FC8C3B8@gmail.com>
References: <HK2P15301MB008352A35CE4FFA43874AD50A6DF0@HK2P15301MB0083.APCP153.PROD.OUTLOOK.COM>
 <FDD724E2-B293-4B4B-A787-E1BA9FC8C3B8@gmail.com>
Message-ID: <22865.16047.276474.128174@stat.math.ethz.ch>

>>>>> peter dalgaard <pdalgd at gmail.com>
>>>>>     on Mon, 26 Jun 2017 13:43:28 +0200 writes:

    > This seems to be due to changes made by Martin Maechler in
    > 2008. Presumably this fixed something, but it escapes my
    > memory.

Yes: The change set (svn -c46441) also contains the following NEWS entry

 BUG FIXES
 
     o	within(<dataframe>, { ... }) now also works when '...' removes
     	more than one column.


    > However, it seems to have broken the equivalence
    > between within.list and within.data.frame, so now

    >   within.list <- within.data.frame

    > does not suffice.

There have been many improvements since then, so maybe we can
change the code so that the above will work again.

Another problem seems that we had no tests of  within.list()
anywhere... so we will have them now.

I've hade an idea that seems to work and even simplify the
code....  will get back to the issue later in the evening.

Martin


    > The crux of the matter seems to be that both the following
    > constructions work for data frames

    >> aq <- head(airquality)
    >> names(aq)
    > [1] "Ozone"   "Solar.R" "Wind"    "Temp"    "Month"   "Day"    
    >> aq[c("Wind","Temp")] <- NULL
    >> aq
    > Ozone Solar.R Month Day
    > 1    41     190     5   1
    > 2    36     118     5   2
    > 3    12     149     5   3
    > 4    18     313     5   4
    > 5    NA      NA     5   5
    > 6    28      NA     5   6
    >> aq <- head(airquality)
    >> aq[c("Wind","Temp")] <- vector("list",2)
    >> aq
    > Ozone Solar.R Month Day
    > 1    41     190     5   1
    > 2    36     118     5   2
    > 3    12     149     5   3
    > 4    18     313     5   4
    > 5    NA      NA     5   5
    > 6    28      NA     5   6

    > However, for lists they differ:

    >> aq <- as.list(head(airquality))
    >> aq[c("Wind","Temp")] <- vector("list",2)
    >> aq
    > $Ozone
    > [1] 41 36 12 18 NA 28

    > $Solar.R
    > [1] 190 118 149 313  NA  NA

    > $Wind
    > NULL

    > $Temp
    > NULL

    > $Month
    > [1] 5 5 5 5 5 5

    > $Day
    > [1] 1 2 3 4 5 6

    >> aq <- as.list(head(airquality))
    >> aq[c("Wind","Temp")] <- NULL
    >> aq
    > $Ozone
    > [1] 41 36 12 18 NA 28

    > $Solar.R
    > [1] 190 118 149 313  NA  NA

    > $Month
    > [1] 5 5 5 5 5 5

    > $Day
    > [1] 1 2 3 4 5 6


    > -pd

    >> On 26 Jun 2017, at 04:40 , Hong Ooi via R-devel <r-devel at r-project.org> wrote:
    >> 
    >> The behaviour of within() with list input changes if you delete 2 or more variables, compared to deleting one:
    >> 
    >> l <- list(x=1, y=2, z=3)
    >> 
    >> within(l,
    >> {
    >> rm(z)
    >> })
    >> #$x
    >> #[1] 1
    >> #
    >> #$y
    >> #[1] 2
    >> 
    >> 
    >> within(l, {
    >> rm(y)
    >> rm(z)
    >> })
    >> #$x
    >> #[1] 1
    >> #
    >> #$y
    >> #NULL
    >> #
    >> #$z
    >> #NULL
    >> 
    >> 
    >> When 2 or more variables are deleted, the list entries are instead set to NULL. Is this intended?
    >> 
    >> ______________________________________________
    >> R-devel at r-project.org mailing list
    >> https://stat.ethz.ch/mailman/listinfo/r-devel

    > -- 
    > Peter Dalgaard, Professor,
    > Center for Statistics, Copenhagen Business School
    > Solbjerg Plads 3, 2000 Frederiksberg, Denmark
    > Phone: (+45)38153501
    > Office: A 4.23
    > Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



[Rd] Odd behaviour in within.list() when deleting 2+ variables
Martin Maechler