help working with date values
Gabor,
These options will work just fine. Thank you
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
Gabor
Grothendieck
<ggrothendieck at gm To
ail.com> Steve_Friedman at nps.gov
cc
04/28/2009 09:39 r-help at r-project.org
AM AST Subject
Re: [R] help working with date
values
Here are a few ways:
xx <- c("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")
# 1 library(zoo) as.numeric(floor(as.yearmon(xx)))
[1] 1985 1985 1985 1985 1985 1985 1985 1985 1985 1985
#2 as.numeric(substr(xx, 1, 4))
[1] 1985 1985 1985 1985 1985 1985 1985 1985 1985 1985
#3 as.numeric(format(as.Date(xx), "%Y"))
[1] 1985 1985 1985 1985 1985 1985 1985 1985 1985 1985
On Tue, Apr 28, 2009 at 9:04 AM, <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" ?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.