Adding a new method to data.table

I work a lot with time series. Most of my manipulations are done with data.table

, but often I have to validate data called by a specific time, and for that I use the method xts

:

> timedata['2014-01-02/2014-01-03']

      

My data data.table

is basically a replica xts

with the first columns index

containing the time:

> dt_timedata <- data.table(index=index(timedata), coredata(timedata))

      

At some point the data got too big, so keeping both or transforming them all the time is not a good option (which it never was), so I'm thinking of the same method for data.table

. However, I could not find any reasonable examples of changing the general method.

I wish it were possible, and if so where could I read about it?

PS Even though I can abviosly use something like

> from <- '2014-01-02'
> to <- '2014-01-03'
> period <- as.POSIXct(c(from, to))
> dt_timedata[index %between% period]

      

it's much less intuitive and pretty, so I'd rather write a new method.

Edit1 (example on request)

require(xts)
require(data.table)
days <- as.POSIXct(c('2014-01-01', '2014-01-02', '2014-01-03', '2014-01-04'), origin='1970-01-01')
timedata <- xts(1:4, days)
dt_timedata <- data.table(index=index(timedata), coredata(timedata))

      

What can I do in xts

:

> timedata['2014-01-01/2014-01-02']
       [,1]
2014-01-01    1
2014-01-02    2

      

I want the same for [.data.table

.

Edit2 (to illustrate what I am doing)

'[.data.table' = function(x, i, ...) {
    if (!missing('i')) {  
        if (all(class(i) == "character")) {
            # do some weird stuff
            return(x[ *some indices subset I just created* ])
        }
    }
    data.table:::'[.data.table'(x, i, ...)
}

      

So basically, if it i

is a symbol and fits my format (checks happen in the "weird stuff" section), I return something and the function never jumps to the last command. Otherwise, nothing happens and I just call

data.table:::'[.data.table'(x, i, ...)

      

And the point is that it breaks expressions like

ind <- as.POSIXct('2014-01-01', origin='1970-01-01')
dt_timedata[index==ind]

      

Here's a trivial example:

require(data.table)
days <- as.POSIXct(c('2014-01-01', '2014-01-02', '2014-01-03', '2014-01-04'), origin='1970-01-01')
dt_timedata <- data.table(index=days, value=1:4)
ind <- as.POSIXct('2014-01-01', origin='1970-01-01')
# now it works
dt_timedata[index==ind]
# new trivial [.data.table
'[.data.table' = function(x, I, ...) {
    data.table:::`[.data.table`(x, I, ...)
}
# and now it doesn't work
dt_timedata[index==ind]

      

+3


source to share


1 answer


Modifying the method to add your own smth smth is very simple:

`[.data.table` = function(...) {
  print("I'm doing smth custom")
  data.table:::`[.data.table`(...)
}

dt = data.table(a = 1:5)
dt[, sum(a)]
#[1] "I'm doing smth custom"
#[1] 15

      

So, just handle the first argument however you like and feed it to a standard function.



Here's an example of handling your edit:

`[.data.table` = function(...) {
  if (try(class(..2), silent = TRUE) == 'character')
    print("boo")
  else
    data.table:::`[.data.table`(...)
}

dt = data.table(a = 1:10)
dt[a == 4]
#   a
#1: 4

dt['sdf']
#[1] "boo"
#[1] "boo"

      

+2


source







All Articles