[R] How to handle missing value as first item in yearmon (zoo package)

Gabor Grothendieck ggrothendieck at gmail.com
Fri Mar 19 15:30:04 CET 2010


There is a bug in as.Date.yearmon and as.Date.yearqtr which is
triggered by certain NAs.  Its now fixed in the development version of
zoo on R-forge.  Note that in your code it only affecting printing of
the yearmon variable.  The yearmon variable itself is correct.

Try this:

source("http://r-forge.r-project.org/plugins/scmsvn/viewcvs.php/*checkout*/pkg/zoo/R/yearmon.R?rev=686&root=zoo")
x <- as.yearmon("March 2010","%B %Y")
z <- yearmon(c(NA, x))
z


On Fri, Mar 19, 2010 at 9:36 AM, Marshall Feldman <marsh at uri.edu> wrote:
> Hi,
>
> Some time series have missing values in the time index. For example
> historical data on business cycles will typically date them from peak to
> trough, but some information may be missing. In most cases, this does not
> cause trouble,
> but if the first date is missing (e.g., we know the date of the first trough
> but not the earlier peak), we want the first element in a list of dates to
> be NA. Using yearmon, this causes trouble. Consider this:
>
>> x <- as.yearmon("March 2010","%B %Y")
>> y <- c(x,NA)
>> z <- c(NA,x)
>> y <- yearmon(y)
>> z <- yearmon(z)
>> y
>        [1] "Mar 2010" NA
>> z
>    Error in charToDate(x) :
>      character string is not in a standard unambiguous format
>
> Can someone explain how to make an object of type yearmon with a missing
> value in its first element?
>
> Thanks.
>
>    Marsh Feldman
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list