Reputation: 1121
Given the code as following.
library(data.table)
dt <- data.table(V1=round(runif(9,100,999),2), V2=rep(1:3,3),
V3=round(runif(9,10,99),2), V4=rep(letters[1:3],3))
setindex(dt,V4)
F1 <- dt[V2==2 & V3>=3, max(V1)]
F2 <- dt[V2==2 & V3>=3, max(V1), on = "V4"]
I am 100% sure that class(dt)
is "data.table, data.frame"
and it runs well with F1, but comes the error of
logical error. i is not a data.table, but 'on' argument is provided.
when F2?
Why? How to solve it?
What I am trying to do is not subsetting(or grouping), but to improve the calculation efficiency with the "on" command which I was told is the keyword for secondary indexing. Many thanks.
I know where I made a mistake. Simply bcs I am using it in the wrong way. i is always a data.table when "on" command is given.
My original purpose is to search for the target efficiently.
condition: V3>=3, and V2==2 target: max(V1)
I can't pass i a condition, but I can make i a data.table as following.
F2 <- dt[V3>=3][V2==2,max(V1), on = "V4"]
it runs perfectly! Thanks guys.
Upvotes: 1
Views: 1339
Reputation: 4910
You're not using the right subset syntax. You must have some objects in the workspace named V2
or V3
. data.table
thinks you're merging them. The i
argument is the first argument to [.data.table
. Replace V2==2 & V3>=3
with (V2==2 & V3>=3)
to refer to the column variables. See here about the subtleties of scoping with i=
as a subset in [.data.table
. The last on
should probably be by
(although you would still have had an error because of the subset syntax).
Upvotes: 2