MichaelChirico / r-bugs

A ⚠️read-only⚠️mirror of https://bugs.r-project.org/
20 stars 0 forks source link

[BUGZILLA #12318] Bug in format.default(): na.encode does not have any effect forlogical, numeric, complex, ... #3411

Closed MichaelChirico closed 4 years ago

MichaelChirico commented 4 years ago

From: Gorjanc Gregor <Gregor.Gorjanc@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::>-lj.si> Hi!

If I use format() on numeric vector, na.encode argument does not have any effect. This was reported before:

It works for other (say character) classes!

format(c("a", NA), na.encode=TRUE)

[1] "a " "NA"

format(c("a", NA), na.encode=FALSE)

[1] "a" NA

I explored this a bit and found out that format.default() uses prettyNum(.Internal(format(...))) for formatting the logical, numeric, complex, ... classes. Simple tests (see bellow) show that .Internal(format()) does not obey the na.encode argument.

## Encode NA as "NA"

.Internal(format(x=c(1, NA), trim=FALSE, digits=NULL, nsmall=0, width=NULL,
adj=1, na.encode=TRUE, scientific=NA))

[1] " 1" "NA"

## Do not encode NA as "NA"

.Internal(format(x=c(1, NA), trim=FALSE, digits=NULL, nsmall=0, width=NULL,
adj=1, na.encode=FALSE, scientific=NA))

[1] " 1" "NA" ## --> no effect

## Does prettyNum encode NA as "NA"

prettyNum(c("1", NA))

[1] "1" NA ## --> not for character

prettyNum(c(1, NA))

[1] "1" "NA" ## --> but it does for numeric

I did all my tests with my Ubuntu box running R

version
           _

platform i486-pc-linux-gnu arch i486 os linux-gnu system i486, linux-gnu status major 2 minor 7.1 year 2008 month 06 day 23 svn rev 45970 language R version.string R version 2.7.1 (2008-06-23)

-- Lep pozdrav / With regards, Gregor Gorjanc ---------------------------------------------------------------------- University of Ljubljana PhD student Biotechnical Faculty www: http://gregor.gorjanc.googlepages.com Zootechnical Department blog: http://ggorjan.blogspot.com Groblje 3 mail: gregor.gorjanc bfro.uni-lj.si <CENSORING FROM DETECTED PHONE NUMBER ONWARDS; SEE BUGZILLA>


METADATA

MichaelChirico commented 4 years ago

NOTES: There are no 'NA strings' here -- see the help page


METADATA

MichaelChirico commented 4 years ago

Audit (from Jitterbug): Fri Aug 8 07:09:09 2008 ripley changed notes Fri Aug 8 05:09:09 2008 ripley moved from incoming to feature&FAQ


METADATA

MichaelChirico commented 4 years ago

From: Prof Brian Ripley <ripley@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::>> Have you read the help page? It says

na.encode: logical: should 'NA' strings be encoded?

No mention of encoding NA numerical values that I can see, and it was never intended to apply to other than strings.

Please do the homework the FAQ asks of you and only use R-bugs if you 'know for certain' -- in particular, do heed

'Rather than jumping to conclusions, show the problem to someone who knows for certain'

On Thu, 7 Aug 2008, Gregor.Gorjanc@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::>-lj.si wrote:

Hi!

If I use format() on numeric vector, na.encode argument does not have any e=
ffect. This
was reported before:
- https://stat.ethz.ch/pipermail/r-help/2007-October/143881.html
- http://tolstoy.newcastle.edu.au/R/e2/devel/06/09/0360.html

It works for other (say character) classes!

> format(c("a", NA), na.encode=3DTRUE)
[1] "a " "NA"

> format(c("a", NA), na.encode=3DFALSE)
[1] "a" NA

I explored this a bit and found out that format.default() uses prettyNum(.I=
nternal(format(...)))
for formatting the logical, numeric, complex, ... classes. Simple tests (se=
e bellow) show that
.Internal(format()) does not obey the na.encode argument.

It works as documented (and coded).



## Encode NA as "NA"
> .Internal(format(x=3Dc(1, NA), trim=3DFALSE, digits=3DNULL, nsmall=3D0, w=
idth=3DNULL, adj=3D1, na.encode=3DTRUE, scientific=3DNA))
[1] " 1" "NA"

## Do not encode NA as "NA"
> .Internal(format(x=3Dc(1, NA), trim=3DFALSE, digits=3DNULL, nsmall=3D0, w=
idth=3DNULL, adj=3D1, na.encode=3DFALSE, scientific=3DNA))
[1] " 1" "NA"
## --> no effect

## Does prettyNum encode NA as "NA"
> prettyNum(c("1", NA))
[1] "1" NA
## --> not for character

> prettyNum(c(1, NA))
[1] "1" "NA"
## --> but it does for numeric

I did all my tests with my Ubuntu box running R

> version
_
platform       i486-pc-linux-gnu
arch           i486
os             linux-gnu
system         i486, linux-gnu
status
major          2
minor          7.1
year           2008
month          06
day            23
svn rev        45970
language       R
version.string R version 2.7.1 (2008-06-23)

--
Lep pozdrav / With regards,
Gregor Gorjanc
----------------------------------------------------------------------
University of Ljubljana     PhD student
Biotechnical Faculty        www: http://gregor.gorjanc.googlepages.com
Zootechnical Department     blog: http://ggorjan.blogspot.com
Groblje 3                   mail: gregor.gorjanc <at> bfro.uni-lj.si
<CENSORING FROM DETECTED PHONE NUMBER ONWARDS; SEE BUGZILLA>

-----------

#### METADATA
 - Comment author - Jitterbug compatibility account
 - Timestamp - 2008-08-09 19:34:45 UTC
MichaelChirico commented 4 years ago

From: Gorjanc Gregor <Gregor.Gorjanc@<::CENSORED -- SEE ORIGINAL ON BUGZILLA::>-lj.si> Hi to all!

Prof Brian Ripley wrote:

Have you read the help page?  It says

na.encode: logical: should 'NA' strings be encoded?

No mention of encoding NA numerical values that I can see, and it was
never intended to apply to other than *strings*.

I did read the help page, however it seems that I have confused the meaning of 'NA' string and 'NA' value. It might be obvious that the first form is meant for strings (charcaters) only and the other for numeric like vectors, but not to me.

IMHO it would be better that na.encode argument would also have an effect for numeric like vectors. Nearly any function in R returns NA values and I expected the same for format, at least when na.encode=FALSE.

Regards, Gregor


METADATA