Reputation: 835
I am new to data.table and think that this is an easy question, but can't seem to find the answer anywhere.
I want to subset a table based on the value of two columns, whose names I know. But I want to compare against a value which I don't know in advance. That is, I want to use a variable for the i portion of DT[]. But I can't seem to figure out how to do it. Everything I see explains how to use a variable for j (i.e. column names), but not for i.
When I just put the name of the variable in, i.e.
setkey(dtpredictions, colA, colB)
nextweek = dtpredictions[J(uservar, weekvar)]
it returns the entire table. Trying to apply the answer to FAQ 1.6, I tried:
nextweek = dtpredictions[J(eval(quote(uservar)), eval(quote(weekvar)))]
and
nextweek = dtpredictions[J(eval(user), eval(week))]
but both still returned the whole table.
I am pretty sure this is very simple, but I am stuck.
EDIT I apologize for not clarifying earlier: I would like to do a binary search, since I need the speedup. I know that I can do a vector scan using ==, but I would prefer not to.
Upvotes: 3
Views: 1139
Reputation: 835
Found the problem - one of my variables had the same name as a column in the table. I actually saw a question about a similar problem here, but didn't even realize that I had that issue. (It was another column in the table, not the one I was subsetting on.)
I changed the name of the variable I was using to subset and now it works.
Upvotes: 3
Reputation: 4615
hmmm...interesting. Does this code seem to work for you? I am not getting the same error. I am using data.table 1.9.3
.
require(data.table)
iris <- data.table(iris)
#Create new categorical variable
set.seed(1)
iris[ , new.var := sample(letters[1:5],150,replace=TRUE)]
#Set keys
setkey(iris,Species,new.var)
#Create variables to reference
check1 <- "setosa"
check2 <- "b"
#Return matches
iris[J(check1,check2)]
And the resulting table:
Sepal.Length Sepal.Width Petal.Length Petal.Width Species new.var
1: 5.1 3.5 1.4 0.2 setosa b
2: 4.9 3.0 1.4 0.2 setosa b
3: 5.0 3.6 1.4 0.2 setosa b
4: 5.4 3.7 1.5 0.2 setosa b
5: 4.3 3.0 1.1 0.1 setosa b
6: 5.7 3.8 1.7 0.3 setosa b
7: 5.1 3.7 1.5 0.4 setosa b
8: 4.8 3.4 1.9 0.2 setosa b
9: 5.0 3.0 1.6 0.2 setosa b
10: 5.2 3.5 1.5 0.2 setosa b
11: 4.7 3.2 1.6 0.2 setosa b
Upvotes: 1
Reputation: 5536
Is this you are looking for?
setkey(dtpredictions, colA, colB)
nextweek <- dtpredictions[colA == uservar & colB == weekvar]
Upvotes: 0