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

2017-06-26 Thread Hong Ooi via R-devel
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@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


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

2017-06-26 Thread peter dalgaard
This seems to be due to changes made by Martin Maechler in 2008. Presumably 
this fixed something, but it escapes my memory.

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.

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
141 190 5   1
236 118 5   2
312 149 5   3
418 313 5   4
5NA  NA 5   5
628  NA 5   6
> aq <- head(airquality)
> aq[c("Wind","Temp")] <- vector("list",2)
> aq
  Ozone Solar.R Month Day
141 190 5   1
236 118 5   2
312 149 5   3
418 313 5   4
5NA  NA 5   5
628  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  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@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@cbs.dk  Priv: pda...@gmail.com

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] Suggestion for optim() in stats package

2017-06-26 Thread Marc Girondot via R-devel

When optim() is used with method="BFGS", the name of parameters within
the vector are transmitted (see below, first example).

When method="Brent", the name of parameter (only one parameter can be
fitted with Brent method) is not transmitted. As there is only one, of
course, we know which parameter it is, but it makes things
non-consistent between methods.

It would be better that same convention is used for different method as
it will permit to make more general use of optim().

Marc

Tested in R-3.4.1

For example, here:

@@@
Method BFGS
@@@

# The names of values in par are transmitted
fitnorm_meansd<-function(par, val) {
   print(par)
   -sum(dnorm(x=val, mean=par["mean"], sd=par["sd"], log = TRUE))
}

val <- rnorm(100, mean=20, sd=2)
p<-c(mean=20, sd=2)
result<-optim(par=p, fn=fitnorm_meansd, val=val, method="BFGS")

The print(par) shows the named vector:
 > result<-optim(par=p, fn=fitnorm_meansd, val=val, method="BFGS")
mean   sd
   202
   mean sd
20.001  2.000
   mean sd
19.999  2.000
   mean sd
20.000  2.001
etc...

@@@
Method Brent
@@@

# The name of value in par is not transmitted

fitnorm_mean<-function(par, val) {
   print(par)
   -sum(dnorm(x=val, mean=par, sd=2, log = TRUE))
}

val <- rnorm(100, mean=20, sd=2)
p<-c(mean=20)
result<-optim(par=p, fn=fitnorm_mean, val=val, method="Brent", lower=10,
upper=30)


The print(par) does not show named vector:
 > result<-optim(par=p, fn=fitnorm_mean, val=val, method="Brent",
lower=10, upper=30)
[1] 17.63932
[1] 22.36068

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


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

2017-06-26 Thread Martin Maechler
> peter dalgaard 
> 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(, { ... }) 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
> 141 190 5   1
> 236 118 5   2
> 312 149 5   3
> 418 313 5   4
> 5NA  NA 5   5
> 628  NA 5   6
>> aq <- head(airquality)
>> aq[c("Wind","Temp")] <- vector("list",2)
>> aq
> Ozone Solar.R Month Day
> 141 190 5   1
> 236 118 5   2
> 312 149 5   3
> 418 313 5   4
> 5NA  NA 5   5
> 628  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  
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@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@cbs.dk  Priv: pda...@gmail.com

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


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

2017-06-26 Thread Peter Dalgaard

> On 26 Jun 2017, at 19:04 , Martin Maechler  wrote:
> 
>> peter dalgaard 
>>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(, { ... }) now also works when '...' removes
>   more than one column.
> 

The odd thing is that the assign-NULL technique used for removing a single 
column, NOW also seems to work for several columns in a data frame, so I wonder 
what the bug was back then...

-pd


> 
>> 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
>> 141 190 5   1
>> 236 118 5   2
>> 312 149 5   3
>> 418 313 5   4
>> 5NA  NA 5   5
>> 628  NA 5   6
>>> aq <- head(airquality)
>>> aq[c("Wind","Temp")] <- vector("list",2)
>>> aq
>> Ozone Solar.R Month Day
>> 141 190 5   1
>> 236 118 5   2
>> 312 149 5   3
>> 418 313 5   4
>> 5NA  NA 5   5
>> 628  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  
>>> 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@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@cbs.dk  Priv: pda...@gmail.com
> 
> 
> 
> 
> 
> 
> 
> 

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

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] suggestion to fix packageDescription() for Windows users

2017-06-26 Thread Nathan Sosnovske via R-devel
I'd be curious to know what others think of Rich's patch. If it is acceptable, 
I can spend time that I was going to look at it this week on another bug.

-Original Message-
From: Rich Calaway 
Sent: Friday, June 23, 2017 6:34 PM
To: Nathan Sosnovske ; Duncan Murdoch 
; Andrie de Vries 
Cc: Ben Marwick ; R-devel Mailing List (r-devel@r-project.org) 

Subject: RE: [Rd] suggestion to fix packageDescription() for Windows users

The following patch is not the most elegant, but it restores the Authors when 
"LC_CTYPE" is set to either "Chinese" or "Arabic":

> Sys.setlocale("LC_CTYPE", "Chinese")
[1] "Chinese (Simplified)_China.936"
> citation("readr")

To cite package ‘readr’ in publications use:

  (2016). readr: Read Tabular Data. R package version 1.0.0.
  https://CRAN.R-project.org/package=readr

A BibTeX entry for LaTeX users is

  @Manual{,
title = {readr: Read Tabular Data},
year = {2016},
note = {R package version 1.0.0},
url = {https://CRAN.R-project.org/package=readr},
  }

ATTENTION: This citation information has been auto-generated from the package 
DESCRIPTION file and may need manual editing, see ‘help("citation")’.

> Sys.setlocale("LC_CTYPE", "Arabic")
[1] "Arabic_Saudi Arabia.1256"
> citation("readr")

To cite package ‘readr’ in publications use:

  (2016). readr: Read Tabular Data. R package version 1.0.0.
  https://CRAN.R-project.org/package=readr

A BibTeX entry for LaTeX users is

  @Manual{,
title = {readr: Read Tabular Data},
year = {2016},
note = {R package version 1.0.0},
url = {https://CRAN.R-project.org/package=readr},
  }

ATTENTION: This citation information has been auto-generated from the package 
DESCRIPTION file and may need manual editing, see ‘help("citation")’.

> citation <- newCitation
> citation("readr")

To cite package ‘readr’ in publications use:

  Hadley Wickham, Jim Hester and Romain Francois (2016). readr: Read
  Tabular Data. R package version 1.0.0.
  https://CRAN.R-project.org/package=readr

A BibTeX entry for LaTeX users is

  @Manual{,
title = {readr: Read Tabular Data},
author = {Hadley Wickham and Jim Hester and Romain Francois},
year = {2016},
note = {R package version 1.0.0},
url = {https://CRAN.R-project.org/package=readr},
  }



The patch is:

Index: citation.R
===
--- citation.R  (revision 72852)
+++ citation.R  (working copy)
@@ -1162,8 +1162,11 @@
 if(dir == "")
 stop(gettextf("package %s not found", sQuote(package)),
  domain = NA)
-meta <- packageDescription(pkg = package,
-   lib.loc = dirname(dir))
+   args <- list(pkg = package, lib.loc = dirname(dir))
+   if (!is.na(enc <- packageDescription(pkg = package, 
lib.loc=dirname(dir), field="Encoding")))
+   args$enc <- enc
+meta <- do.call("packageDescription", args=args)
+
 ## if(is.null(auto)): Use default auto-citation if no CITATION
 ## available.
 citfile <- file.path(dir, "CITATION")


Nathan says he can look into this further next week...

Cheers,

Rich Calaway
Microsoft R Product Team
24/1341
+1 (425) 4219919 X19919

-Original Message-
From: R-devel [mailto:r-devel-boun...@r-project.org] On Behalf Of Nathan 
Sosnovske via R-devel
Sent: Friday, June 23, 2017 7:36 AM
To: Duncan Murdoch ; Andrie de Vries 

Cc: r-devel@r-project.org; Ben Marwick 
Subject: Re: [Rd] suggestion to fix packageDescription() for Windows users

Hi Duncan,

I'm guessing I'll be able to look at this over the weekend/next week (probably 
closer to next week). It is on my list of things to do and I've just had a few 
other prior commitments that I have to finish first.

Sorry for the delay. I'll chime in with a status update next week.

Nathan

-Original Message-
From: R-devel [mailto:r-devel-boun...@r-project.org] On Behalf Of Duncan Murdoch
Sent: Friday, June 23, 2017 5:16 AM
To: Andrie de Vries 
Cc: r-devel@r-project.org; Ben Marwick 
Subject: Re: [Rd] suggestion to fix packageDescription() for Windows users

On 18/06/2017 5:57 AM, Andrie de Vries wrote:
> Hi, Duncan
>
> i have forwarded this thread to Nathan, who promised to look into it.

Any progress on this?

Duncan Murdoch

>
> Andrie
>
> On 17 Jun 2017 17:26, "Duncan Murdoch"  > wrote:
>
> On 17/06/2017 9:13 AM, Ben Marwick wrote:
>
> Hi Duncan,
>
> Thanks for your reply. Yes, it does seem to be specific to the CTYPE
> setting to Chinese on Windows. If I set it to English using
> Sys.setlocale() there is no problem, then back to Chinese and the
> authors disappear:
>
> Sys.setlocale("LC_ALL","English")
> citation("readr")
>
>
> Thanks, that makes the problem reproducible.  I'll submit it as a
> bug report.  Maybe someone from Microsoft will fix it.
>
> Duncan Murdoch
>
>
> #' To

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

2017-06-26 Thread Martin Maechler
> "PD" == Peter Dalgaard 
> on Mon, 26 Jun 2017 20:12:38 +0200 writes:

>> On 26 Jun 2017, at 19:04 , Martin Maechler
>>  wrote:
>> 
>>> peter dalgaard  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(, { ... }) now also works when '...'
>> removes more than one column.
>> 

> The odd thing is that the assign-NULL technique used for
> removing a single column, NOW also seems to work for
> several columns in a data frame, so I wonder what the
> bug was back then...

It did not work back then:  We have had lots of improvements in
 [.data.frame in these almost 9 years.

Indeed, the fix I've committed reverts almost to the previous
first version of  within.data.frame  (which is from Peter
Dalgaard, for those who don't know).

Martin


>>> 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
  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@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@cbs.dk Priv: pda...@gmail.com
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> 

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

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


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

2017-06-26 Thread peter dalgaard

> On 26 Jun 2017, at 21:56 , Martin Maechler  wrote:
> 
> 
> Indeed, the fix I've committed reverts almost to the previous
> first version of  within.data.frame  (which is from Peter
> Dalgaard, for those who don't know).
> 

Great foresight on my part there, eh? ;-)

-p

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

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel