I am trying to figure out how can I execute logical operators when I use indexing in the data.table package in R ?
The following is an example. I am making a datatable with the name dt . and then make var2 as the key in my datatable :
> dt = data.table(var1 = rep(LETTERS[1:5],2), var2 = seq(1,20, 2), var3 = ceiling(rnorm(10, 3, 2))) > dt var1 var2 var3 1: A 1 5 2: B 3 3 3: C 5 0 4: D 7 6 5: E 9 3 6: A 11 4 7: B 13 2 8: C 15 1 9: D 17 3 10: E 19 7 > setkey(dt, var2)
So now I want to identify all the values ββin my already defined key (var2) that are less than 10 ( <10) . Performing the following attempts will give me errors .
> dt[ < 10] Error: unexpected '<' in "dt[ <" > dt[ .< 10] Error in eval(expr, envir, enclos) : object '.' not found > dt[ .(< 10)]
my expectation:
var1 var2 var3 1: A 11 4 2: B 13 2 3: C 15 1 4: D 17 3 5: E 19 7
By the way, I know that just by making dt[var2 <10] , I will get the result. BUT, please think that I want to get the concept of indexing in data.table and understand and know how to do this without calling key(var2) in each of my commands!
Any help with explanation is greatly appreciated.
source share