简体   繁体   中英

What's the difference between the types of objects in R?

I have seem many definitions about what is the type of an object in R. There is the mode(), the typeof() and the class(). Which appears to be the type of the object. So there is already three definitions of type of an object. There is also vector, array, matrix, dataframe, list, which is also defined as "type of an object" (at least in some websites, like this one https://www.programcreek.com/2014/01/vector-array-list-and-data-frame-in-r/ ).

So, my question is: what is the type of an object in R? Is it the mode? Is it the class? Is it the output of typeof()? Is it something among list/matrix/dataframe/array?

I'm a bit confused. If someone asks me what is the type of x? Should I answer something like 'integer' or should I say 'numeric' or should I say 'a vector'?

Hope the following can be of help:

First of all: the tricky part is that in R there are 3 different OO (object-oriented) systems that are used, and a 4th system, called base types which underlies the other OO systems: the latter provides (C-) code as building blocks for the other OO systems. Depending on the system, the way that for example classes and methods are defined, differs. The S3 system should meet the needs of most R programming, so I'll try to explain based on this system (for information regarding the other systems, visit the source of the information underneath: H. Wickham, Advanced R ; R tutorial by Kelly Black ; R for Data Science, Import, tidy, transform, visualize, and model data by H. Wickham and G. Grolemund, O'Reilly, 2017).

A quick background before I answer your question(s): 1) Base Type & 2) Attributes :

1)

Underlying every R object is a C structure (or so-called struct ) that defines how that object is stored in memory . The struct includes the: A) information needed for memory management; B) contents of the object, C) most importantly, a type . This is the base type of an R object. Base types are not really an object system because only the R core team can create new types. As a result, new base types are added rarely.

The most common base types are vectors (atomic vectors and lists). But also environments and functions etc. are types of base objects.

Vector Types: Hierarchy

2)

Any vector can contain arbitrary additional metadata through its attributes . You can think of attributes as a named list of vectors that can be attached to any object (many of the objects that are created within an R session have attributes associated with them). You can get and set individual attribute values with (attr() or see them all at once with attributes(). There are 3 very important attributes that are used to implement fundamental parts of R: A) Names —> are used to name the elements of a vector B) Dimensions —> make a vector behave like a matrix or array C) Class —> used to implement the S3 OO system. in general, it defines the 'behavior' of objects. It does that by describing its attributes and its relationship to other classes. Generic functions (the special type of function that carries out computations in the S3 OO system (1/3 of the 3 existing R OO systems that differ in how classed and methods are defined)) are key to OO programming in R, because they make functions behave differently for different classes of input. A class is also used when selecting methods, which are functions that behave differently depending on the class of their input (also see below). S3 has no formal definition of a class. To make an object an instance of a class you take an existing base object and set the class attribute, using the class command.

Your Q's answered, based on S3:

Methods: In S3 methods belong to generic functions, based on the class of the first argument (all methods are functions, but not all functions are methods)/. S3 methods do not belong to classes or objects. UseMethod() will call a specific method. You can list all the methods for a generic with methods(). Mode() and storage.mode() are functions that are just aliases of the names that are returned by typeof(). These functions only exist for S compatibility purposes. You may as well ignore them, or if you want to read what exactly they do: read/decipher their source code.

Typeof() will give you an object's base type (so, as described above, a vector, an environment, a function, etc.). So, when you ask “what's the type of x”, then vector , as you suggested would be correct . You should not say numeric or integer, but if your vector is one is one the 6 types: logical, integer , double *, character, complex, or raw*, you could call it an “atomic vector” , and if it is integer or double, you could specifically call it an “atomic vector of the numeric type” . One note: unfortunately the names of base types are not used consistently throughout R, and type and the corresponding “is” function may use different names, see the code beneath (source: R tutorial by Kelly Black )

# The type of a function is "closure"
f <- function() {}
typeof(f)
#> [1] "closure"
is.function(f)
#> [1] TRUE

# The type of a primitive function is "builtin"
typeof(sum)
#> [1] "builtin"
is.primitive(sum)
#> [1] TRUE
    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM