[R] Counting dates in arbitrary ranges

Mauricio Zambrano hzambran.newsgroups at gmail.com
Tue Jan 18 11:36:09 CET 2011


if you only want to count the number of days in an arbitrary time
period, you may try:

> install.packages("hydroTSM", dependencies=TRUE)

> library(hydroTSM)

>  dip(from="2007-05-01", to="2009-09-10", out.type="nmbr")

I hope this helps.

Kinds,

Mauricio

-- 
===============================
Linux user #454569 -- Ubuntu user #17469
===============================

"2007-05-01"), as.Date("2009-09-10"

2011/1/18 Dennis Murphy <djmuser at gmail.com>:
> Hi:
>
> Perhaps you were looking for something like this:
>
> table(cut(mydata[[1]], breaks=seq(from = as.Date("2008-06-26"),
>                                                        to =
> as.Date("2009-06-26"), by = 'month')))
>
> 2008-06-26 2008-07-26 2008-08-26 2008-09-26 2008-10-26 2008-11-26 2008-12-26
>
>        30         31         31         30         31         30         31
>
> 2009-01-26 2009-02-26 2009-03-26 2009-04-26 2009-05-26
>        31         28         31         30         31
>
> Considering that your data range from May 2007 to mid-November 2009, a way
> to generate monthly tables (or any set of common breaks you want) for all
> components of the list can be done as follows:
>
> f <- function(x) table(cut(x, breaks = seq(from = as.Date('2007-05-01'),
>                                            to = as.Date('2009-12-01'), by =
> 'month')))
> lapply(mydata, f)
>
> This is simply intended to get you started in case you wanted to map your
> problem across multiple list components.
>
> HTH,
> Dennis
>
> On Mon, Jan 17, 2011 at 11:16 PM, Simon Kiss <simonjkiss at yahoo.ca> wrote:
>
>> Dear Colleagues,
>> I have a data set that looks as below. I'd like to count the number of
>> dates in a series of arbitrary ranges (breaks) i.e. not pre-defined breaks
>> such as months, quarters or years. table(format()) produces ideally
>> formatted output, but table() does not appear to accept arbitrary ranges.
>> I also tried converting the dates to numeric and using histogram to try to
>> get the data, but that doesn't work either.  Cut appears to accept an
>> arbitrary range, but I could only get it to produce NAs.
>>
>> Any suggestions? Yours, Simon Kiss
>>
>> mydata<-list(x=seq(as.Date("2007-05-01"), as.Date("2009-09-10"),"days"),
>> y=seq(as.Date("2007-06-16"), as.Date("2009-11-12"),"days"))
>> table(format(mydata[[1]], "%Y"))
>> t_1<-hist(as.numeric(mydata[[1]], breaks=c("14056", "14421")))$counts
>> cut(mydata[[1]], breaks=c(as.Date("2008-06-26"), ("2009=06-26")))
>>
>>
>> *********************************
>> Simon J. Kiss, PhD
>> Assistant Professor, Wilfrid Laurier University
>> 73 George Street
>> Brantford, Ontario, Canada
>> N3T 2C9
>> Cell: +1 519 761 7606
>>
>> ______________________________________________
>> 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.
>>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list