[R] help working with date values
Uwe Ligges
ligges at statistik.tu-dortmund.de
Tue Apr 28 15:33:14 CEST 2009
Steve_Friedman at nps.gov wrote:
> My data contains a variable "observation_date" and it contains values as:
>
> 1985-09-02
> 1985-09-15
> 1985-07-31
> 1985-09-02
>
>
> I need to process data annually rather than daily, therefore I'm trying to
> 1) either extract the first 4 digits from this field and use them as a new
> variable "year"
year <- as.numeric(format((strptime(observation_date, "%Y-%m-%d")), "%Y"))
or
year <- as.numeric(substr(observation_date, 1, 4))
Uwe Ligges
> or 2) keep the variable as it is and process the analysis
> using the first 4 digits of the observation_date field.
>
> I'm not sure how to do either one of these approaches. I've looked in the
> R-archive help pages, date, strsplit and a few others
>
>> attach(gator)
>> observation_date[1:10]
> [1] 1985-09-02 1985-09-16 1985-07-31 1985-07-31 1985-09-02 1985-08-26
> 1985-07-31 1985-08-26 1985-09-02 1985-09-16
>
>> as.date(observation_date)
> Error in as.date(observation_date) : Cannot coerce to date format
>
>> mode(observation_date)
> "numeric"
>
>> y <- as.character(observation_date)
> [1] "1985-09-02" "1985-09-16" "1985-07-31" "1985-07-31" "1985-09-02"
> "1985-08-26" "1985-07-31" "1985-08-26" "1985-09-02" "1985-09-16"
>
> < y.date <- as.date(y)
> [1] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> on and on
> and on ...
>
>
>> x <- strsplit(observation_date, "-")
> Error in strsplit(observation_date, "-") : non-character argument
>
>
> All help is greatly appreciated.
>
> Thanks
> Steve
>
>
>
> Steve Friedman Ph. D.
> Spatial Statistical Analyst
> Everglades and Dry Tortugas National Park
> 950 N Krome Ave (3rd Floor)
> Homestead, Florida 33034
>
> Steve_Friedman at nps.gov
> Office (305) 224 - 4282
> Fax (305) 224 - 4147
>
> ______________________________________________
> 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