[R] aggregation question

Gabor Grothendieck ggrothendieck at gmail.com
Thu Jul 7 14:09:12 CEST 2011


On Thu, Jul 7, 2011 at 5:41 AM, Dr Eberhard Lisse <el at lisse.na> wrote:
> Hi,
>
> I am reading payment data like so
>
> 2010-01-01,100.00
> 2010-01-04,100.00
> ...
> 2011-01-01,200.00
> 2011-01-07,100.00
>
> and plot it aggregated per month like so
>
> library(zoo)
> df <- read.csv("daily.csv", colClasses=c(d="Date",s="numeric"))
> z <- zoo(df$s, df$d)
> z.mo <- aggregate(z, as.yearmon, sum)
> barplot(z.mo, col="darkblue")
>
> How do I get the monthly aggregated payments in different colors
> next to each other (ie for each year in a different color with the x
> axis showing the months)?
>

Read it in with read.zoo aggregating at the same time to yearmon class
and then issue the appropriate lattice barchart command:

Lines <- "Date,Value
2010-01-01,100.00
2010-01-04,100.00
2010-02-04,100.00
2011-01-01,200.00
2011-01-07,100.00
2011-02-07,100.00"

library(zoo)
z <- read.zoo(textConnection(Lines), header = TRUE, sep = ",",
	FUN = as.yearmon, aggregate = sum)

library(lattice)
year <- factor(as.numeric(floor(time(z))))
value <- coredata(z)
month <- coredata(cycle(z))
barchart(value ~ month | year, horiz = FALSE, col = 1:12, origin = 0)



-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list