6

I want to use := operator from data.table without loading data.table. For example for the following data.table, I want to add another column called error:

DT <- data.table::data.table(station = rep(1:1,52560), mod = rnorm(1*52560),obs = rnorm(1*52560))

If I do the following everything goes well, however, I am puzzled how it worked without referring to package data.table (the data.table library is not loaded)?!!

DT[ , `:=`(error  = mod - obs)]

How can I rewrite the above line using data.table::::= ?!!

smci
  • 32,567
  • 20
  • 113
  • 146
newbie
  • 757
  • 1
  • 9
  • 19
  • 2
    what is the purpose of creating a data table object if you aren't using any of its features – rawr Feb 12 '16 at 00:19
  • 2
    If you want to include all references to data.table functions, you would need `[.data.table` as well. But I agree with rawr, just load the package. – Rich Scriven Feb 12 '16 at 00:20
  • Isn't `:=` just `data.table::set` in a roundabout fashion? – thelatemail Feb 12 '16 at 00:21
  • I am going to use this in another package and preferably I want not to load data.table. – newbie Feb 12 '16 at 00:22
  • 2
    If you are going to use data.table syntax in a package, I would recommend you import the entire package. I've done it. It's much safer, less worries. – Rich Scriven Feb 12 '16 at 00:24
  • @thelatemail could you give an example how to use, I do find any example. However, the main question here is how it works eventhough I have not loaded the library – newbie Feb 12 '16 at 00:24
  • 1
    @RichardScriven I am importing the package, I have no other choice since data.table will not work if I do not import. Would importing alone take care of this? – newbie Feb 12 '16 at 00:25
  • 1
    @newbie - `data.table::set(DT, i=NULL, j="blah", value=1)` - accessing column names will probably require `[.data.table` anyway though. – thelatemail Feb 12 '16 at 00:39

1 Answers1

2

Not sure I understand correctly, but regarding :

I am going to use this in another package and preferably I want not to load data.table. – newbie

I am importing the package, I have no other choice since data.table will not work if I do not import. Would importing alone take care of this? – newbie

Yes Import data.table rather than Depend on it from your package. You may have tried that and it didn't work because of this common issue. See solution here :

Using data.table package inside my own package

Community
  • 1
  • 1
Matt Dowle
  • 58,872
  • 22
  • 166
  • 224
  • I have already import data.table to my package. I know for some of the commands such as as.data.table I have to use data.table::as.data.table but some other commands like DT[ , `:=`(error = mod - obs)], it will work automatically without me specifying it is from data.table package. That is what I am puzzled, why this happens? – newbie Feb 12 '16 at 15:24
  • "import data.table" is too imprecise wording given the detail of the linked question. You do not have use the prefix in data.table::as.data.table. I don't follow what you're asking. Why what happens? Suspect English is not your first language. Please take 2 hours to read https://github.com/Rdatatable/data.table/wiki/Support and _all_ its links and confirm you've done so. – Matt Dowle Feb 12 '16 at 20:30