How can I subset a data.table by using a variable, when the variable name is identical to an existing column name in the data.table? It works with get("varname",pos = 1), but is there are more robust/flexible solution?
library(data.table)
my_data_frame <- data.frame(
"V1"=c("A","B","C","A"),
"V2"=c(1, 2, 3, 4),
stringsAsFactors = FALSE
)
V1 <- "A"
my_data_table <- as.data.table(my_data_frame)
# Can I improve this a bit? I want rows where V1 == "A", but use V1 in the statement
my_data_table[ my_data_table$V1 == get("V1", pos = 1), ]
Renaming V1 is not an option.
UPDATE: I do not consider this a 100% duplicate. The accepted answer for this question is not acceptable for my question, since it uses explicit get which I do not want to use, as stated in the comments.
j, but then we can use the 'dot dot notation':d[ , d[V1 == ..V1]]- Henrikmy_data_table[V1 == get("V1", envir = .GlobalEnv)]- Jaapdata.tableversion >= v1.10.2? - Henrikd[d[, .I[V1 == ..V1]]]- chinsoon12