Hi r-help-boun...@r-project.org napsal dne 28.04.2009 15:04:01:
> > 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 new.date <- as.Date(observation_date, format="%Y-%m-%d") and then you can use format for coercing to years format(new.date, "%Y") or cut(new.date, "year") Regards Petr > > > 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_fried...@nps.gov > Office (305) 224 - 4282 > Fax (305) 224 - 4147 > > ______________________________________________ > R-help@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. ______________________________________________ R-help@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.