Skip to content
Prev 173289 / 398506 Next

Question about datatypes/plotting issue

You need to convert W$Date into a real date variable. At the moment it  
is just a character variable.

 > str(W)
'data.frame':	265 obs. of  23 variables:
  $ Date            : Factor w/ 265 levels " ","April 1987",..: 1 90  
68 156 2 178 134 ...
  $ AZ.Phoenix      : Factor w/ 236 levels "","100.00","100.43",..:  
236 1 1 1 1 1 1 1 1 1 ...
  $ CA.Los.Angeles  : Factor w/ 260 levels "100.00","100.02",..: 260  
113 114 115 116 ...
  $ CA.San.Diego    : Factor w/ 261 levels "100.00","101.07",..: 261  
109 110 111 112 ...
  $ CA.San.Francisco: Factor w/ 256 levels "100.00","102.70",..: 256  
108 109 110 111 ...
.(output trimmed)
.
.
?Date  # not the variable name, the R class name
?format.Date
?strptime

Unfortunately I seem to be at one of the many limits to my knowledge:
This code behaves in the manner I expected:

 > format(Sys.time(), "%a %b %d %X %Y %Z")
[1] "Tue Mar 10 22:19:28 2009 EDT"
 > strptime(format(Sys.time(), "%a %b %d %X %Y %Z"), format="%a %b %d  
%X %Y %Z")
[1] "2009-03-10 22:20:04"

Whereas this code does not:
 > format(Sys.Date(), "%B %Y")
[1] "March 2009"
 > as.Date(format(Sys.Date(), "%B %Y"), "%B %Y")
# would have assumed one was the inverse of the other, but ...

[1] NA

For some reason I cannot get the space delimited Month-YYYY combo to  
convert. I can getother  space delimited formats to work for input or  
output:
 > as.Date("03 1998", "%M %Y")
[1] "1998-03-10"

 > format(Sys.Date(), "%B %Y")
[1] "March 2009"

Puzzled;