MLEN
MLEN

Reputation: 2561

Passing a vector to data.table with index in R

How can I subset iris_dtusing the vector subz and achieve the same results as the line above

library(data.table)

iris_dt <- data.table(iris)
setindex(iris_dt, Sepal.Length)
setindex(iris_dt, Species)



iris_dt[.(6.7, 'virginica'), on = c('Sepal.Length', 'Species')]

subz <- c(6.7, 'virginica')

Upvotes: 0

Views: 55

Answers (1)

s_baldur
s_baldur

Reputation: 33498

You need to set key, and avoid using c because of data type coercion.

setkey(iris_dt, Sepal.Length, Species)
subz <- list(6.7, 'virginica')
iris_dt[subz]

Upvotes: 1

Related Questions