Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the real meaning about 'Everything that exists is an object' in R?

Tags:

I saw:

“To understand computations in R, two slogans are helpful:

• Everything that exists is an object.
• Everything that happens is a function call."

— John Chambers

But I just found:

a <- 2 is.object(a) # FALSE 

Actually, if a variable is a pure base type, it's result is.object() would be FALSE. So it should not be an object.

So what's the real meaning about 'Everything that exists is an object' in R?

like image 447
Colin Ji Avatar asked Dec 19 '15 23:12

Colin Ji


People also ask

Is everything an object in R?

Objects in R Also, everything in R is an object and to know more look at Data types in R. They also can have their attributes like class, attributes,dimnnames, names, etc.

What does object mean in R?

In fact, everything in R is an object. An object is a data structure having some attributes and methods which act on its attributes. Class is a blueprint for the object. We can think of class like a sketch (prototype) of a house. It contains all the details about the floors, doors, windows etc.

How do you determine if an object exists in R?

Check if an Object of the Specified Name is Defined or not in R Programming – exists() Function. exists() function in R Programming Language is used to check if an object with the names specified in the argument of the function is defined or not. It returns TRUE if the object is found.

How do you make an object in R studio?

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.


1 Answers

The function is.object seems only to look if the object has a "class" attribute. So it has not the same meaning as in the slogan.

For instance:

x <- 1 attributes(x) # it does not have a class attribute NULL is.object(x) [1] FALSE class(x) <- "my_class" attributes(x) # now it has a class attribute $class [1] "my_class" is.object(x) [1] TRUE 

Now, trying to answer your real question, about the slogan, this is how I would put it. Everything that exists in R is an object in the sense that it is a kind of data structure that can be manipulated. I think this is better understood with functions and expressions, which are not usually thought as data.

Taking a quote from Chambers (2008):

The central computation in R is a function call, defined by the function object itself and the objects that are supplied as the arguments. In the functional programming model, the result is defined by another object, the value of the call. Hence the traditional motto of the S language: everything is an object—the arguments, the value, and in fact the function and the call itself: All of these are defined as objects. Think of objects as collections of data of all kinds. The data contained and the way the data is organized depend on the class from which the object was generated.

Take this expression for example mean(rnorm(100), trim = 0.9). Until it is is evaluated, it is an object very much like any other. So you can change its elements just like you would do it with a list. For instance:

call <- substitute(mean(rnorm(100), trim = 0.9)) call[[2]] <- substitute(rt(100,2 )) call mean(rt(100, 2), trim = 0.9) 

Or take a function, like rnorm:

rnorm function (n, mean = 0, sd = 1)  .Call(C_rnorm, n, mean, sd) <environment: namespace:stats> 

You can change its default arguments just like a simple object, like a list, too:

formals(rnorm)[2] <- 100 rnorm function (n, mean = 100, sd = 1)  .Call(C_rnorm, n, mean, sd) <environment: namespace:stats> 

Taking one more time from Chambers (2008):

The key concept is that expressions for evaluation are themselves objects; in the traditional motto of the S language, everything is an object. Evaluation consists of taking the object representing an expression and returning the object that is the value of that expression.

So going back to our call example, the call is an object which represents another object. When evaluated, it becomes that other object, which in this case is the numeric vector with one number: -0.008138572.

set.seed(1) eval(call) [1] -0.008138572 

And that would take us to the second slogan, which you did not mention, but usually comes together with the first one: "Everything that happens is a function call".

Taking again from Chambers (2008), he actually qualifies this statement a little bit:

Nearly everything that happens in R results from a function call. Therefore, basic programming centers on creating and refining functions.

So what that means is that almost every transformation of data that happens in R is a function call. Even a simple thing, like a parenthesis, is a function in R.

So taking the parenthesis like an example, you can actually redefine it to do things like this:

`(` <- function(x) x + 1 (1) [1] 2 

Which is not a good idea but illustrates the point. So I guess this is how I would sum it up: Everything that exists in R is an object because they are data which can be manipulated. And (almost) everything that happens is a function call, which is an evaluation of this object which gives you another object.

like image 92
Carlos Cinelli Avatar answered Oct 24 '22 07:10

Carlos Cinelli