Matt Bannert
Matt Bannert

Reputation: 28264

Handling datefield in indexes?

I have a dataset containing some variable1 and a quarterly datefield. I would like to table(variable1) by datefield. Plus I wonder how to use the datefield in an index like

 attach(myds)
 table(variable1[datefield == "1984-01-01"])

In SQL I would do Something like:

 SELECT * FROM myds GROUP BY YEAR(datefield) 

but how can I do it in R? I know i could turn it to a character field and strsplit it, but i would prefer to work with real date fields – if possible.

Upvotes: 0

Views: 67

Answers (2)

hadley
hadley

Reputation: 103898

Have look at year and round_date in lubridate.

Upvotes: 3

Matt Bannert
Matt Bannert

Reputation: 28264

Partly I can help myself:

format(datefield, "%Y")

helps me to get all the years without string splitting. Still I wonder how the rest (quarterly grouping is possible).

lubridate from Hadley is even better than that, because it returns a numeric object.

Still, my problem to find the best way to find the best way to get frequencies by date. So far I tried:

aggregate(myvar,list(datefield),FUN=table)

and ended up with a list like 1984-04-01 17, 16, 5, 8, 7, 49

This is okay, but still a little bit cumbersome to process. Let's say myvar is some kind of id and I would like to count the appearance of ids over time. Is there an alternative to looping?

Upvotes: 0

Related Questions