Home > other >  Disambiguating a variable name in a function when a column with the same name as the variable exists
Disambiguating a variable name in a function when a column with the same name as the variable exists

Time:06-28

I have a function with a variable named source. The function works properly, but if the data frame on which the function is applied has a column also named source, it doesn't work.

A simple example with dplyr and filtering: These two following lines works, but they filter based on the column (I want to filter on the variable name defined in the function):

corpus %>% dplyr::filter(!!source=="027021335")
corpus %>% dplyr::filter(source=="027021335")

This following line work and properly use the variable defined in the function:

corpus %>% dplyr::filter(!!rlang::sym(source)=="027021335")

How to achieve the same thing using data.table()? I have tried numerous combination of c(), get() and .. without managing to make it work. I thought that corpus[get(source)=="027021335"] should have worked but it is not the case as it returns a first argument has length > 1 error.

Edit: I think one possible reason I get this error is that in addition to source as a variable and as a column name, there is a source() function is base r.

Corpus using dput(corpus):

structure(list(idref = c("027021335", "182132870", "221468579", 
"034574654", "069546592", "159340950", "169800458", "028529413", 
"076605442", "026762889"), iddoc = c(97466L, 101100L, 103772L, 
110039L, 134077L, 55693L, 38787L, 39304L, 73483L, 74350L), nom = c("Méhaut", 
"Favre", "Guerdjikova", "Diebolt", "Giraud-Héraud", "Charlier", 
"Moumni", "Henni", "Bonnel", "Callens"), prenom = c("Philippe", 
"Karine", "Ani", "Claude", "Eric", "Christophe", "Nicolas", "Ahmed", 
"Patrick", "Stéphane"), order = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 
0), role = c("supervisor", "supervisor", "supervisor", "supervisor", 
"supervisor", "supervisor", "supervisor", "supervisor", "supervisor", 
"supervisor"), Annee_soutenance = c("2011", "2014", "2018", "2009", 
"2006", "2015", "2012", "2008", "2009", "2010"), source = c("as.character(idref)", 
"as.character(idref)", "as.character(idref)", "as.character(idref)", 
"as.character(idref)", "as.character(idref)", "as.character(idref)", 
"as.character(idref)", "as.character(idref)", "as.character(idref)"
), time_variable = c("as.character(idref)", "as.character(idref)", 
"as.character(idref)", "as.character(idref)", "as.character(idref)", 
"as.character(idref)", "as.character(idref)", "as.character(idref)", 
"as.character(idref)", "as.character(idref)")), row.names = c(NA, 
-10L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x000001ee3a911ef0>)

CodePudding user response:

With data.table development version (1.14.3), this can be done with the new env argument, see programming on data.table:

data.table::update.dev.pkg()
source = "idref"
corpus[source=="027021335",env=list(source=source)]

       idref iddoc    nom   prenom order       role Annee_soutenance              source       time_variable
1: 027021335 97466 Méhaut Philippe     0 supervisor             2011 as.character(idref) as.character(idref)
  • Related