[R] Hist with Dates

Christopher W Ryan cryan at binghamton.edu
Tue Jan 15 18:20:14 CET 2013


If all you want is frequencies, could you use table()?

dd <- c("2001-01-02", "2001-01-02",  "2001-01-02", "2001-01-02",
"2001-01-02", "2001-01-02", "2001-01-02", "2001-01-02", "2001-01-02",
"2001-01-03", "2001-01-03", "2001-01-03", "2001-01-03", "2001-01-03",
"2001-01-03", "2001-01-03", "2001-01-03", "2001-01-04", "2001-01-04")
ddd <- as.Date(dd)
str(ddd)
table(ddd)
as.numeric(table(ddd))

--Chris Ryan
SUNY Upstate Medical University
Binghamton, NY USA

On Tue, Jan 15, 2013 at 7:45 AM, Dominic Roye <dominic.roye at gmail.com> wrote:
> Hello R-Helpers,
>
>
> I would like to generate the frequencies of dates (data.RData).
>
> str(data)
>  Date[1:43748], format: "2001-01-01" "2001-01-01" "2001-01-01" "2001-01-01"
> ...
>
>
> test <- hist(data,"days",freq=T)
>> test$counts
>    [1] 27 17 12 15  6  4  5 13  5 13 15  9  7 10 11 21 10 10  9 11 15  7 10
> 14
>
>
> but the count is wrong, because i have only 9 times 01/01/2001 etc.
>
>> data[1:30]
>  [1] "2001-01-01" "2001-01-01" "2001-01-01" "2001-01-01" "2001-01-01"
>  [6] "2001-01-01" "2001-01-01" "2001-01-01" "2001-01-01"
>
> "2001-01-02"
> [11] "2001-01-02" "2001-01-02" "2001-01-02" "2001-01-02" "2001-01-02"
> [16] "2001-01-02" "2001-01-02" "2001-01-02" "2001-01-02" "2001-01-02"
> [21] "2001-01-03" "2001-01-03" "2001-01-03" "2001-01-03" "2001-01-03"
> [26] "2001-01-03" "2001-01-03" "2001-01-03" "2001-01-03" "2001-01-03"
>
>
>
> Now, when i give R the breaks.
>
>> br <-
> seq(as.Date("31.12.2000",format="%d.%m.%Y"),as.Date("01.01.2012",format="%d.%m.%Y"),by=1)
>> test2<- hist(data,br,freq=T)
>> test2$counts[1:10]
>  [1] 12 15 17 12 15  6  4  5 13  5
>
>
> In this case, neither get the right counts but limiting i get the correct
> counts:
>
>> test3<- hist(data[1:50],br[1:6],freq=T)
>> test3$counts
> [1]  9 11 13  9  8
>
>
> I hope someone can help me with this problem.
>
>
> Thank you very much!!
>
> Best regards,
>
> Dominic
>
> ______________________________________________
> 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