[Rd] na.encode in format for Date and POSIXt classes

Gregor Gorjanc gregor.gorjanc at bfro.uni-lj.si
Tue Sep 26 01:59:47 CEST 2006


Hello!

na.encode does not have any effect on format of NA values of Date and
POSIXct (POSIXlt?) "atomic" classes in a data.frame. Here is the example
(the same in R 2.3.1 and 2.5.0 (2006-09-19 r39409)):

testData <- data.frame(num=c(NA, 2.6),
                       int=c(1, NA),
                       fac=factor(c(NA, "abc")),
                       cha=c("a", NA),
                       dat=as.Date(c("1900-1-1", NA)),
                       POS=as.POSIXct(strptime(c("1900-1-1 01:01:01",
                                                 NA),
                                      format="%Y-%m-%d %H:%M:%S")))
testData$cha <- as.character(testData$cha)
testData
  num int  fac  cha        dat                 POS
1  NA   1 <NA>    a 1900-01-01 1900-01-01 01:01:01
2 2.6  NA  abc <NA>       <NA>                <NA>

format(testData)
  num int fac cha        dat                 POS
1  NA   1  NA   a 1900-01-01 1900-01-01 01:01:01
2 2.6  NA abc  NA       <NA>                <NA>
                        ^^^^                ^^^^

format(testData, na.encode=FALSE)
  num int  fac  cha        dat                 POS
1  NA   1 <NA>    a 1900-01-01 1900-01-01 01:01:01
2 2.6  NA  abc <NA>       <NA>                <NA>

format(testData, na.encode=TRUE)
  num int fac cha        dat                 POS
1  NA   1  NA   a 1900-01-01 1900-01-01 01:01:01
2 2.6  NA abc  NA       <NA>                <NA>
                        ^^^^                ^^^^

After a brief look into format.data.frame, format.POSIXct and
format.POSIXlt I notice that na.encode is not used at all in (last line
of format.POSIXlt)

 .Internal(format.POSIXlt(x, format, usetz))

but I get lost from this point forward. I guess that the same applies to
format.Date as this coerces Date to POSIXlt before formatting.

Additionally, I would suggest change in format help page for argument
na.encode. It says now

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

If I am not wrong, this applies only to non-numeric columns. Something
like the following might be more accurate:

na.encode: logical: should 'NA' strings be encoded (applies only to
non-numeric)?

-- 
Lep pozdrav / With regards,
    Gregor Gorjanc
----------------------------------------------------------------------
University of Ljubljana     PhD student
Biotechnical Faculty
Zootechnical Department     URI: http://www.bfro.uni-lj.si/MR/ggorjan
Groblje 3                   mail: gregor.gorjanc <at> bfro.uni-lj.si

SI-1230 Domzale             tel: +386 (0)1 72 17 861
Slovenia, Europe            fax: +386 (0)1 72 17 888

----------------------------------------------------------------------
"One must learn by doing the thing; for though you think you know it,
 you have no certainty until you try." Sophocles ~ 450 B.C.




More information about the R-devel mailing list