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
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.
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. >
Hi r-help-bounces at r-project.org napsal dne 28.04.2009 15:04:01:> > My data contains a variable "observation_date" and it contains valuesas:> > 1985-09-02 > 1985-09-15 > 1985-07-31 > 1985-09-02 > > > I need to process data annually rather than daily, therefore I'm tryingto> 1) either extract the first 4 digits from this field and use them as anew> variable "year" or 2) keep the variable as it is and process theanalysis> 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 inthe> 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 formatnew.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 andon> 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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.