开发者

Handling datefield in indexes?

开发者 https://www.devze.com 2023-02-11 11:01 出处:网络
I have a dataset contai开发者_StackOverflow中文版ning some variable1 and a quarterly datefield. I would like to

I have a dataset contai开发者_StackOverflow中文版ning 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.


Have look at year and round_date in lubridate.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号