Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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!

like image 914
Tomas Avatar asked Feb 20 '12 21:02

Tomas


People also ask

How do I identify an object in R?

To get type of a value or variable or object in R programming, call typeof() function and pass the value/variable to it.

How do you define an object in R?

In RStudio, typing Alt + - (push Alt at the same time as the - key) will write <- in a single keystroke. Here are a few rules as of how to name objects in R. Objects can be given any name such as x , current_temperature , or subject_id . You want your object names to be explicit and not too long.

How do you check if an object exists?

Method 1: Using the typeof operator The typeof operator returns the type of the variable on which it is called as a string. The return string for any object that does not exist is “undefined”. This can be used to check if an object exists or not, as a non-existing object will always return “undefined”.

Why does R say object not found?

6.2 Error: object not foundThis error usually occurs when your R Markdown document refers to an object that has not been defined in an R chunk at or before that chunk. You'll frequently see this when you've forgotten to copy code from your R Console sandbox back into a chunk in R Markdown.


1 Answers

You want exists():

R> exists("somethingUnknown") [1] FALSE R> somethingUnknown <- 42 R> exists("somethingUnknown") [1] TRUE R>  
like image 117
Dirk Eddelbuettel Avatar answered Sep 17 '22 22:09

Dirk Eddelbuettel