Many thanks for the recent answers to my question about pattern recognition. The hint to "grep" and the "pattern" argument brought me a big deal forward. # Now, I have a data frame: DATE <- c("1930-01-01", "1930-01-01", "1930-02-01", "1931-01-01", "1931-02-01", "1931-03-01") # almost all months until "1999-12-01". Day is always 01, the same months actually occur several times. FREQ <- rep(1,6) ex<- data.frame(DATE,FREQ) # I can aggregate it like this: ex <- tapply(ex$FREQ, ex$DATE, sum) # or use"grep" to extract a single year: # ex[grep(pattern="^1931", ex$DATE),] # But how can I aggregate the years such that a barplot would show me the FREQ per year and not per month? barplot(as.vector(ex)) # Sorry for the bad example, of course my data.frame is much bigger and the barplot gives me a nice monthly time series of events, but I really only want the occurrences per year, not per month. Grateful for your advice! Friderike
Try this: aggregate(ex$FREQ, list(Year = substr(ex$DATE, 1, 4)), sum) or aggregate(ex$FREQ, list(Year = format(as.Date(ex$DATE), "%Y")), sum) On Mon, Jul 14, 2008 at 9:37 AM, Oehler, Friderike (AGPP) <Friderike.Oehler at fao.org> wrote:> Many thanks for the recent answers to my question about pattern recognition. > The hint to "grep" and the "pattern" argument brought me a big deal forward. > > # Now, I have a data frame: > > DATE <- c("1930-01-01", "1930-01-01", "1930-02-01", "1931-01-01", > "1931-02-01", "1931-03-01") # almost all months until "1999-12-01". Day is > always 01, the same months actually occur several times. > FREQ <- rep(1,6) > ex<- data.frame(DATE,FREQ) > > # I can aggregate it like this: > > ex <- tapply(ex$FREQ, ex$DATE, sum) > > # or use"grep" to extract a single year: > # ex[grep(pattern="^1931", ex$DATE),] > > # But how can I aggregate the years such that a barplot would show me the > FREQ per year and not per month? > > barplot(as.vector(ex)) > > # Sorry for the bad example, of course my data.frame is much bigger and the > barplot gives me a nice monthly time series of events, but I really only want > the occurrences per year, not per month. > > Grateful for your advice! > Friderike > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Hi r-help-bounces at r-project.org napsal dne 14.07.2008 14:37:36:> Many thanks for the recent answers to my question about patternrecognition.> The hint to "grep" and the "pattern" argument brought me a big dealforward.> > # Now, I have a data frame: > > DATE <- c("1930-01-01", "1930-01-01", "1930-02-01", "1931-01-01", > "1931-02-01", "1931-03-01") # almost all months until "1999-12-01". Dayis> always 01, the same months actually occur several times. > FREQ <- rep(1,6) > ex<- data.frame(DATE,FREQ) >Your DATE is not in Date format. See as.Date ex$Date<-as.Date(ex$DATE)> # I can aggregate it like this: > > ex <- tapply(ex$FREQ, ex$DATE, sum)ex1<-tapply(ex$FREQ, cut(ex$Date, "years"), sum) ex1<-tapply(ex$FREQ, format(ex$Date, "%Y"), sum) gives you yearly aggregates> > # or use"grep" to extract a single year: > # ex[grep(pattern="^1931", ex$DATE),] > > # But how can I aggregate the years such that a barplot would show methe> FREQ per year and not per month? > > barplot(as.vector(ex))barplot(ex1) could be what you want. Regards Petr> > # Sorry for the bad example, of course my data.frame is much bigger andthe> barplot gives me a nice monthly time series of events, but I really onlywant> the occurrences per year, not per month. > > Grateful for your advice! > Friderike > > ______________________________________________ > 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.