Question: how to check if a variable exists in r?
Answer: > detach(df) > exists("df$varName") [1] FALSE. Instead of using exists, you can use in or any from the base package to determine if a variable is defined in a data frame:
> "varName" %in% names(df) [1] TRUE. > any(names(df) == "varName") [1] TRUE. ... 
> "varName" %in% colnames(df) [1] TRUE. > any(colnames(df) == "varName")