If you use attach, it is easy to tell if a variable exists. You can simply use exists to check:
However, if you don’t use attach (and I find you generally don’t want to), this simple solution doesn’t work.
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:
Or to determine if a variable is defined in a matrix: