1

I have following r code which uses dplyr. Due to large data size, we want to use data.table.

test <- function(Act, mac, type, thisYear){
    Act %>%
    mutate_(var = type) %>%
    filter(var == mac) %>%
    filter(floor_date(as.Date(submit_ts), 'year') == thisYear)
}

Act is as follows

| submit_ts     | col1          | col2  |
| ------------- |---------------|-------|
| '2015-01-01'  | 'x'           | 1000  |
| '2015-01-01'  | 'y'           |  200  |
| '2015-01-01'  | 'x'           |  200  |

basically function can works as follows

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  |

How should I do it using data.table ?

Kush Patel
  • 3,685
  • 5
  • 42
  • 65
  • 1
    In case you haven't seen them before, these are some instructions on how to make a reproducible example: http://stackoverflow.com/a/28481250/ – Frank Feb 01 '17 at 17:47
  • Also, I am not able to reproduce your output. I use `lubridate_1.6.0` – akrun Feb 01 '17 at 17:50

1 Answers1

3

We can do a similar approach in data.table with

library(data.table)
library(lubridate)
test1 <- function(Act, mac, type){
    setnames(setDT(Act), type, "var")[
       var==mac & year(floor_date(as.Date(submit_ts), "year"))==thisYear]
 }

test1(dat, 2, "val")
#    submit_ts var
#1: 2013-05-05   2
#2: 2013-05-12   2

NOTE: The floor_date does not return a yyyy year.

data

dat <- data.frame(submit_ts= c("2013-05-05", "2012-05-10", "2013-05-12"),
                 val = c(2, 1, 2), stringsAsFactors=FALSE)
thisYear <- 2013
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662
  • type is column name that we are passing in function. We use mac as filtering option for type column. How will filter it?? – Kush Patel Feb 01 '17 at 17:10
  • 1
    @KppatelPatel What is `thisYear` ? It seems to be not an argument in the function. It is better to show a small reproducible example for others to check the code – akrun Feb 01 '17 at 17:15
  • that is just year like '2014', or '2015' or similar yyyy format – Kush Patel Feb 01 '17 at 17:17
  • 2
    @KppatelPatel Please just edit a reproducible example into your post, one that can be run in a new R session to illustrate your question. – Frank Feb 01 '17 at 17:18
  • @KppatelPatel I think your function doesn't work as you intended because check the `floor_date` output with your 4 digit 'thisYear' – akrun Feb 01 '17 at 17:26