How to check if object (variable) is defined in R?

322

I'd like to check if some variable is defined in R - without getting an error. How can I do this?

My attempts (not successful):

> is.na(ooxx)
Error: object 'ooxx' not found
> is.finite(ooxx)
Error: object 'ooxx' not found

Thanks!

This question is tagged with r

~ Asked on 2012-02-20 21:46:47

The Best Answer is


484

You want exists():

R> exists("somethingUnknown")
[1] FALSE
R> somethingUnknown <- 42
R> exists("somethingUnknown")
[1] TRUE
R> 

~ Answered on 2012-02-20 21:51:18


120

See ?exists, for some definition of "...is defined". E.g.

> exists("foo")
[1] FALSE
> foo <- 1:10
> exists("foo")
[1] TRUE

~ Answered on 2012-02-20 21:50:27


Most Viewed Questions: