I know exactly what everyone will say "name your variables properly!", but please consider this problem.
If there is a variable (external to any DT) which has an identical name to a column in a DT how can we reliably refer to both inside DT[ ]
Here's a self-contained example.
#define the sample ID that we are interested in
sample_id <- "sample-3"
#create some toy data
ids <- paste0("sample-",1:5)
dt <- data.table(sample_id=ids)
dt[,data:=rnorm(.N)]
#subset the DT using the sample_id that we are interested in
dt[sample_id==sample_id]
We get this output
> dt[sample_id==sample_id]
sample_id data
1: sample-1 -0.75094006
2: sample-2 0.85408100
3: sample-3 0.02002767
4: sample-4 1.20461499
5: sample-5 -0.46026177
But I would like this
> dt[sample_id==sample_id]
sample_id data
1: sample-3 0.02002767
Are there any special symbols or functions that can be used so DT knows I'm referring to an external var?
Or do I have to rename my external var (or column) Thanks for reading, cheers.
If you want to do it without setting keys, you could specifically look for the variable in the parent frame:
dt[sample_id == get("sample_id", envir = parent.frame())]
#> sample_id data
#> 1: sample-3 1.547372
We can use .GlobalEnv
and extract the object
dt[sample_id == .GlobalEnv$sample_id]
# sample_id data
#1: sample-3 0.2044901
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With