R is an object-oriented language?

18

A theoretical question of who uses (and does not use) r is whether this programming language is (or is not) object oriented. I've seen comments in SOen that said the following:

r is not object oriented, but a functional language, but with OO language traits.

This way:

  • r is an object-oriented, functional language or an aggregate of the two? Why?
  • What does it mean to say that r is vectorized? And, why is this feature present in r so important compared to other programming languages?
asked by anonymous 05.12.2018 / 01:38

1 answer

20

The sentence quoted above SOen is correct. According to John Chambers, creator of ,

  

Everything in the R is an object.

     

Everything that happens in R is the calling of a function.

This creates the curious situation that a function is itself an object.

See the difference between the in the (,

import pandas as pd df = pd.DataFrame({ 'A' : 1., 'B' : pd.Timestamp('20130102'), 'C' : pd.Categorical(["test","train","test","train"])}) df A B C 0 1.0 2013-01-02 test 1 1.0 2013-01-02 train 2 1.0 2013-01-02 test 3 1.0 2013-01-02 train

and in (package , more functional):

df <- data.frame(A = 1,
                 B = as.Date("2013-01-02"),
                 C = c("test","train","test","train"))
df
  A          B     C
1 1 2013-01-02  test
2 1 2013-01-02 train
3 1 2013-01-02  test
4 1 2013-01-02 train

Now that we have our df in pandas and r-base , we can see the difference between functional and object-oriented approaches to, for example, checking the type of information contained in each column. >

In object orientation, the object itself has contained a property (sometimes a method) that allows us to do this.

df.dtypes

A           float64
B    datetime64[ns]
C          category
dtype: object

In the more functional orientation, this information is not accessed by the object itself, but by a function. That is, the "method" does not "live" within the object, but outside and independent of it.

class(df)
[1] "data.frame"

It happens that the class() function only brings us the last information that pandas has brought (the last line). In R , to see the column class, we must apply the function separately for each column. This is done via Map (or more commonly, sapply() ), which is a common feature to find in functional languages. So we have

sapply(df, class) # ou Map(class, df)
        A         B         C 
"numeric"    "Date"  "factor"

The vectorization in R only refers to the fact that R is able to relate two vectors of different sizes. In this way, it is not necessary to write a loop, for example, to add a vector of 5 numbers with a vector of 1 or 2 numbers.

1:5 + 1
[1] 2 3 4 5 6
1:5 + 1:2
[1] 2 4 4 6 6
Warning message:
In 1:5 + 1:2 :
  longer object length is not a multiple of shorter object length

As you can see from the warning in the second example above, this vectorization may have some mishaps. In order not to further extend this response, I recommend reading this answer

    
05.12.2018 / 11:51