[Rd] Methods for format() are not consistent with NA values (PR#12385)

From: <Gregor.Gorjanc_at_bfro.uni-lj.si>
Date: Thu, 07 Aug 2008 12:21:45 +0200 (CEST)


Hi!

Methods for format() are not consistent when applied to vectors having NA values. Some methods encode NA values to "NA" by default, while other do not. It would be much more easier to write other functions if these functions would all behave consistently.

Examples produced with 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)

testData <- data.frame(

                       fac1=3Dfactor(c(NA, letters[1:9], "hjh")),
                       fac2=3Dfactor(c(letters[6:15], NA)),
                       cha1=3Dc(letters[17:26], NA),
                       cha2=3Dc(NA, "longer", letters[25:17]),
                       stringsAsFactors=3DFALSE)
levels(testData$fac1) <- c(levels(testData$fac1), "unusedLevel")
testData$Date <- as.Date("1900-1-1")
testData$Date[2] <- NA
testData$POSIXt <- as.POSIXct(strptime("1900-1-1 01:01:01",
                                       format=3D"%Y-%m-%d %H:%M:%S"))
testData$POSIXt[5] <- NA

## --- Character ---

> format(testData$cha1)
[1] "q " "r " "s " "t " "u " "v " "w " "x " "y " "z " "NA"

> format(testData$cha1, na.encode=3DTRUE)
[1] "q " "r " "s " "t " "u " "v " "w " "x " "y " "z " "NA"

> format(testData$cha1, na.encode=3DFALSE)
[1] "q" "r" "s" "t" "u" "v" "w" "x" "y" "z" NA

## --- Factor ---

> format(testData$fac1)
[1] "NA " "a " "b " "c " "d " "e " "f " "g " "h " "i " "hjh"

> format(testData$fac1, na.encode=3DTRUE)
[1] "NA " "a " "b " "c " "d " "e " "f " "g " "h " "i " "hjh"

> format(testData$fac1, na.encode=3DFALSE)
[1] NA "a " "b " "c " "d " "e " "f " "g " "h " "i " "hjh"

## --- Numeric et al. ---

## na.encode has no effect

## --- Date ---

> format(testData$Date)
[1] "1900-01-01" NA "1900-01-01" "1900-01-01" "1900-01-01"
[6] "1900-01-01" "1900-01-01" "1900-01-01" "1900-01-01" "1900-01-01"
[11] "1900-01-01"

> format(testData$Date, na.encode=3DTRUE)
[1] "1900-01-01" NA "1900-01-01" "1900-01-01" "1900-01-01"
[6] "1900-01-01" "1900-01-01" "1900-01-01" "1900-01-01" "1900-01-01"
[11] "1900-01-01"

> format(testData$Date, na.encode=3DFALSE)
[1] "1900-01-01" NA "1900-01-01" "1900-01-01" "1900-01-01"
[6] "1900-01-01" "1900-01-01" "1900-01-01" "1900-01-01" "1900-01-01"
[11] "1900-01-01"

## --- POSIXt ---

> format(testData$POSIXt)
[1] "1900-01-01 01:01:01" "1900-01-01 01:01:01" "1900-01-01 01:01:01"
[4] "1900-01-01 01:01:01" NA "1900-01-01 01:01:01"
[7] "1900-01-01 01:01:01" "1900-01-01 01:01:01" "1900-01-01 01:01:01"
[10] "1900-01-01 01:01:01" "1900-01-01 01:01:01"

> format(testData$POSIXt, na.encode=3DTRUE)
[1] "1900-01-01 01:01:01" "1900-01-01 01:01:01" "1900-01-01 01:01:01"
[4] "1900-01-01 01:01:01" NA "1900-01-01 01:01:01"
[7] "1900-01-01 01:01:01" "1900-01-01 01:01:01" "1900-01-01 01:01:01"
[10] "1900-01-01 01:01:01" "1900-01-01 01:01:01"

> format(testData$POSIXt, na.encode=3DFALSE)
[1] "1900-01-01 01:01:01" "1900-01-01 01:01:01" "1900-01-01 01:01:01"
[4] "1900-01-01 01:01:01" NA "1900-01-01 01:01:01"
[7] "1900-01-01 01:01:01" "1900-01-01 01:01:01" "1900-01-01 01:01:01"
[10] "1900-01-01 01:01:01" "1900-01-01 01:01:01"

--
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
SI-1230 Domzale             fax: +386 (0)1 72 17 888
Slovenia, Europe            tel: +386 (0)1 72 17 861

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Thu 07 Aug 2008 - 10:39:15 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 07 Aug 2008 - 11:36:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive