[R] NAs in chron objects

Markus Jantti mjantti at ra.abo.fi
Fri May 18 16:23:42 CEST 2001


Dear All:

I am having some trouble with date variables when NAs are present. The
following example illustrates (see below for output of version):

> library(chron)
> minor.test <- c( "NA", "NA", "1988-02-08",
+                 "1988-02-08", "NA", "1987-11-23", "1988-09-03", "1991-10-15",
+                 "1992-01-14", "1992-02-18")
> # data in yyyy-mm-dd format
> minor.test[1:10]
 [1] "NA"         "NA"         "1988-02-08" "1988-02-08" "NA"        
 [6] "1987-11-23" "1988-09-03" "1991-10-15" "1992-01-14" "1992-02-18"
> # This works
> dates(minor.test[1:2], format = "y-m-d")
[1] NA NA
Warning messages: 
1: NAs introduced by coercion 
2: NAs introduced by coercion 
3: NAs introduced by coercion 
> # this fails
> dates(minor.test[1:4], format = "y-m-d")
Error in if (any(i)) y[i] <- ifelse(y[i] < cut.off, y[i] + century[2],  : 
	missing value where logical needed
In addition: Warning messages: 
1: NAs introduced by coercion 
2: NAs introduced by coercion 
3: NAs introduced by coercion 
> # as does this
> dates(minor.test, format = "y-m-d")
Error in if (any(i)) y[i] <- ifelse(y[i] < cut.off, y[i] + century[2],  : 
	missing value where logical needed
In addition: Warning messages: 
1: NAs introduced by coercion 
2: NAs introduced by coercion 
3: NAs introduced by coercion 
> # this works
> dates(na.omit(minor.test), format = "y-m-d")
[1] 88-02-08 88-02-08 87-11-23 88-09-03 91-10-15 92-01-14 92-02-18


dates() correctly produces the first NA's but becomes confused when
strings with times start appearing. The below examples suggest the
POSIXct class in the base package also has some trouble.

> as.POSIXct(minor.test[4])
[1] "1988-02-08 EET"
> as.POSIXct(minor.test[1:10])
Error in fromchar(x) : character string is not in a standard unambiguous format
> as.POSIXct(na.omit(minor.test[1:10]))
[1] "1988-02-08 EET"  "1988-02-08 EET"  "1987-11-23 EET"  "1988-09-03 EEST"
[5] "1991-10-15 EET"  "1992-01-14 EET"  "1992-02-18 EET" 

My problem is that the data are contained in a PostgreSQL database and
I am accessing them using the RPgSQL package. There are typically
several data variables in my data sets and in many cases the dates are
missing. Thus, using na.omit is possible but this problem seems to
preclude letting RPgSQL do the type conversion.  I have temporarily
solved the problem by just returning the postgres date type as
character, this is a little unsatisfactory.

> version
         _                
platform i686-pc-linux-gnu
arch     i686             
os       linux-gnu        
system   i686, linux-gnu  
status                    
major    1                
minor    2.3              
year     2001             
month    04               
day      26               
language R     

Any help would be appreciated.



Regards,

Markus

-- 
Markus Jantti				|	Department of Statistics
markus.jantti at abo.fi			|	Abo Akademi University
http://www.abo.fi/~mjantti		|	FIN 20500 Turku, Finland
358-9-643 747  (Home/Voice)		|	358-2-2154 161	(Office/Voice)
					|	358-2-2154 677	(Office/Fax)
PGP public key: http://www.abo.fi/~mjantti/pubring.asc
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list