【发布时间】:2017-02-01 16:58:22
【问题描述】:
我有以下使用 dplyr 的 r 代码。 由于数据量大,我们想使用data.table。
test <- function(Act, mac, type, thisYear){
Act %>%
mutate_(var = type) %>%
filter(var == mac) %>%
filter(floor_date(as.Date(submit_ts), 'year') == thisYear)
}
行为如下
| submit_ts | col1 | col2 |
| ------------- |---------------|-------|
| '2015-01-01' | 'x' | 1000 |
| '2015-01-01' | 'y' | 200 |
| '2015-01-01' | 'x' | 200 |
基本功能如下
test(act, 'x', 'col1', 2015)
result is as follows
| submit_ts | col1 | col2 |
| ------------- |---------------|-------|
| '2015-01-01' | 'x' | 1000 |
| '2015-01-01' | 'x' | 200 |
test(act, 200, 'col2', 2015)
result is as follows
| submit_ts | col1 | col2 |
| ------------- |---------------|-------|
| '2015-01-01' | 'y' | 200 |
| '2015-01-01' | 'x' | 200 |
我应该如何使用 data.table 呢?
【问题讨论】:
-
如果您以前没有见过它们,这些是有关如何制作可重现示例的一些说明:stackoverflow.com/a/28481250
-
另外,我无法重现您的输出。我用
lubridate_1.6.0
标签: r data.table dplyr