Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reliably refer to column in X where it's name is identical to another variable

Tags:

r

data.table

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.

like image 503
jeales Avatar asked Mar 02 '23 04:03

jeales


2 Answers

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
like image 146
Allan Cameron Avatar answered Mar 05 '23 00:03

Allan Cameron


We can use .GlobalEnv and extract the object

dt[sample_id == .GlobalEnv$sample_id]
#   sample_id      data
#1:  sample-3 0.2044901
like image 27
akrun Avatar answered Mar 04 '23 23:03

akrun