Assign class PCICt to dataframe r

Viewed 505

I have a large set of NetCDF files with historical weather data averaged by month. The time dimension unit is "Days since 1600-01-01" and uses a 360-day calendar. I used the PCICt package to correctly format the time dimension according to the 360-day calendar. My questions are:

  1. I get the following error message when trying to write the data to a data frame or save it to an output file: Error in as.data.frame.default(x[[i]], optional = TRUE) : cannot coerce class ""PCICt"" to a data.frame

  2. Can I extract year and month from this PCICt data?

Any help would be much appreciated.

#packages used
require("PCICt")
require("RNetCDF")

#dput output assigned to "ex" variable (units "Days since 1600-01-01")
ex <- structure(c(1095, 1125, 1155, 1185, 1215, 1245, 1275, 1305, 1335, 
1365, 1395, 1425), .Dim = 12L)

#used PCICt to correctly format date stamp
cal <- "360_day"
origin <- ("1600-01-01")
seconds.per.day <- 86400
origin.pcict <- as.PCICt(origin,cal)
ex_pcict <- origin.pcict + (ex * seconds.per.day)

#result of PCICt code
> print(ex_pcict)
 [1] "1603-01-16" "1603-02-16" "1603-03-16" "1603-04-16"
 [5] "1603-05-16" "1603-06-16" "1603-07-16" "1603-08-16"
 [9] "1603-09-16" "1603-10-16" "1603-11-16" "1603-12-16"

#class definition
> class(ex_pcict)
[1] "PCICt"

#attempt to write to data.frame and resulting error message
> data.frame(date = ex_pcict)
Error in as.data.frame.default(x[[i]], optional = TRUE) : 
  cannot coerce class ""PCICt"" to a data.frame

[SOLVED] After sleeping on my problem, I came to the office this morning and started playing with class conversions, eventually getting a date class with which I could work:

ex_char <- as.character.PCICt(ex_pcict)

ex_date <- as.Date(ex_char)

And then I could write "ex_date" to a data.frame and export as a .txt file. I also used as.numeric(format(ex_date, "%m") and "%Y" to extract month and year, respectively.

0 Answers
Related