[R] (no subject)

Dirk Eddelbuettel edd at debian.org
Wed Sep 3 21:34:53 CEST 2008


On 3 September 2008 at 20:00, Dr Eberhard W Lisse wrote:
| Hi,
| 
| I am getting accumulated data from PostgreSQL, ie for every day in
| which a condition is true I get the number (count) of cases. Starting
| date is 2008-01-01 and end day the last day for which the condition
| is true (which is not necessarily today).
| 
| I obviously do not get records (dates) with count = 0, in other words
| this is not a complete list of every day since 2008-01-01.
| 
| Now I want I plot this, with the sum on the Y axis and the months
| on the X axis, preferably as a line drawing.
| 
| Any ideas?

Sure. It's straightforward if you convert your date data to actual Date
types. 

So for argument's sake:

> rawData <- data.frame(x = c("2008-01-01", "2008-01-21", "2008-02-15", "2008-03-08", "2008-04-20", "2008-05-10", "2008-06-20"), y = c(4, 6, 8, 5, 7, 2 ,1))
> rawData
           x y
1 2008-01-01 4
2 2008-01-21 6
3 2008-02-15 8
4 2008-03-08 5
5 2008-04-20 7
6 2008-05-10 2
7 2008-06-20 1
> rawData[,"x"] <- as.Date(rawData[,"x"])
> rawData[,"yCum"] <- cumsum(rawData[, "y"])
> with(rawData, plot(x, yCum, main="Some text", type='l'))

The key is the as.Date(). If your date characters have a different format,
specify it there.  See help(as.Date).

If you want to aggregates, slice, dice, summarise by time-unit (like months
or weeks), make this a zoo object and study the three excellent vignettes in
the zoo package.  R is pretty good at calculating on dates and times provided
it is given the data in the right form.

Hth, Dirk

-- 
Three out of two people have difficulties with fractions.



More information about the R-help mailing list