Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding difference between attr(x, "class") and class(x)

Background

I'm looking at the jazz package proposed by Romain François. Romain defines a function is_bare_vector with the following syntax:

is_bare_vector <- function(x) {
  is_vector(x) && !is.object(x) && is.null(attr(x, "class"))
}

Question

For: x <- 1:

attr(x, "class")
# NULL

Whereas:

class(x)
# [1] "numeric"

I want to understand why those two functions provide different answers? The help on ?attr references ?class

?attr

Note that some attributes (namely class, comment, dim, dimnames, names, row.names and tsp) are treated specially and have restrictions on the values which can be set. (Note that this is not true of levels which should be set for factors via the levels replacement function.)

?class

Many R objects have a class attribute, a character vector giving the names of the classes from which the object inherits. (Functions oldClass and oldClass<- get and set the attribute, which can also be done directly.)

like image 496
Konrad Avatar asked Feb 23 '19 13:02

Konrad


People also ask

What is class and attributes?

The class attribute specifies one or more classnames for an element. The class attribute is mostly used to point to a class in a style sheet. However, it can also be used by a JavaScript (via the HTML DOM) to make changes to HTML elements with a specified class.

What is the difference between class and instance attributes?

Differences Between Class and Instance Attributes The difference is that class attributes are shared by all instances. When you change the value of a class attribute, it will affect all instances that share the same exact value. The attribute of an instance on the other hand is unique to that instance.

What is the main difference between attributes and methods for a data type?

A variable stored in an instance or class is called an attribute. A function stored in an instance or class is called a method.

What are the different attributes of a Python class?

There are two kinds of valid attribute names: data attributes and methods. The other kind of instance attribute reference is a method. A method is a function that “belongs to” an object. (In Python, the term method is not unique to class instances: other object types can have methods as well.


1 Answers

You just need to read on a little further in help("class"):

If the object does not have a class attribute, it has an implicit class, notably "matrix", "array", "function" or "numeric" or the result of typeof(x) (which is similar to mode(x))

Apparently class() will also return the implicit class if the class attribute is NULL; let's examine the C source code -- we see that if the class attribute is of length 0, it gets the implicit class.:

SEXP R_data_class(SEXP obj, Rboolean singleString)
{
    SEXP value, klass = getAttrib(obj, R_ClassSymbol);
    int n = length(klass);
    if(n == 1 || (n > 0 && !singleString))
    return(klass);
    if(n == 0) {
    SEXP dim = getAttrib(obj, R_DimSymbol);
    int nd = length(dim);
    if(nd > 0) {
        if(nd == 2)
        klass = mkChar("matrix");
        else
        klass = mkChar("array");
    }
    else {
      SEXPTYPE t = TYPEOF(obj);
      switch(t) {
      case CLOSXP: case SPECIALSXP: case BUILTINSXP:
        klass = mkChar("function");
        break;
      case REALSXP:
        klass = mkChar("numeric");
        break;
      case SYMSXP:
        klass = mkChar("name");
        break;
      case LANGSXP:
        klass = lang2str(obj, t);
        break;
      default:
        klass = type2str(t);
      }
    }
    }
    else
    klass = asChar(klass);
    PROTECT(klass);
    value = ScalarString(klass);
    UNPROTECT(1);
    return value;
}
like image 124
duckmayr Avatar answered Sep 18 '22 12:09

duckmayr