Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of all possible classes of R objects

Tags:

r

Assuming a fresh R installation, a clean .Rprofile, no additional packages loaded, and no previous calls of the kind class(x) <- "new.class": What are the possible return values of class(x)?

like image 753
sieste Avatar asked Oct 26 '25 15:10

sieste


2 Answers

Possible values of class in R

The question is: What are the possible values class can take in R? In R, users can define their own classes (as many as you want). Try creating a class for object x using class(x) <- "new.class". But, at a clean install, R starts with this list of basic classes.

Inspect the class of an object using class().

Atomic classes

Classes help describe the type of data contained in a vector. Coercion (changing classes) moving up the subsequent list is safe—it will preserve information. But changing class downward may lose information.

  • character c("hello", "world")
  • complex c(as.complex(-1), 2i)
  • numeric c(as.integer(10), 3L)
  • integer c(100, .5)
  • logical c(TRUE, FALSE)

Other common classes

Classes also help describe the logical structure of the data. (Note: type is the physical structure of the data, and sometimes the class is the same as the type thanks to inheritance.)

  • atomic vectors: class(my.atomic.vector) will return one of the value from the list above, all elements are homogenous
  • matrix: a vector with special 2-dimension attributes
  • array: a vector with special n-dimension attributes
  • list: elements can be from heterogeneous classes
  • data frame: a list with special properties
like image 65
Danielle Avatar answered Oct 29 '25 05:10

Danielle


Find defined classes in R

I had the same question and to me a the best way to find out the defined classes was to simply type in is. and see what appears in the autocompletion menu. Like this:

is.

As you can see you get a list which even considers the loaded packages. Here I have ggplot2 loaded and the corresponding classes are shown in the menu, too.

like image 37
Igor stands with Ukraine Avatar answered Oct 29 '25 04:10

Igor stands with Ukraine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!