Reputation: 995
I R, Is there a way to different other possible classes of a variable
For example, data iris
has num
and Factor
So irrespective of any data sets, can we see what all classes the variable can take?
>str(iris)
'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
Upvotes: 0
Views: 57
Reputation: 174586
It sounds as though you want to know whether there is a way to enumerate all the different available classes in R.
We have to be clear in our nomenclature here. In R, a "class" is simply an attribute assigned to an object. This attribute can be read to decide what methods are available to use on the object. This is further complicated by there being three distinct object-oriented systems available in base R, all of which allow us to define our own classes.
At its simplest, we can use the S3 system to arbitrarily define classes like this:
class(iris$Species) <- "NewClass"
Giving us
class(iris$Species)
#> [1] "NewClass"
and
str(iris)
#> 'data.frame': 150 obs. of 5 variables:
#> $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
#> $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#> $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#> $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#> $ Species : 'NewClass' int 1 1 1 1 1 1 1 1 1 1 ...
#> ..- attr(*, "levels")= chr [1:3] "setosa" "versicolor" "virginica"
Since you (and package writers) are free to create new classes and their associated methods, the number of available classes is essentially infinite and cannot be enumerated. For example, we can change the print method for "NewClass" to something pretty useless:
print.NewClass <- function(x) print("Have a nice day!")
So now when we try to look at Species
in the console we get:
iris$Species
[1] "Have a nice day!"
However, underneath the hood, there is different concept called type, and there are only a finite number of these types available in R, determined by their SEXTYPE representation in the underlying C code.
data("iris")
typeof(iris$Species)
#> [1] "integer"
The currently available types in R are "logical", "integer", "double", "complex", "character", "raw", "list", "NULL", "closure", "special", "builtin", "environment", "S4", "symbol", "pairlist", "promise", "language", "char", "...", "any", "expression", "externalptr", "bytecode" and "weakref", as listed in the help file for ?typeof
. Some of these are never used directly by end users.
You will note that “factor” is not a basic type at all, but actually a class.
There is also the concept of the "mode" or “storage mode” of an object, but this is effectively determined by its type
My guess is that you are looking for a particular data type to suit some requirement, and wanted to know if there are any built in classes in R that fit the bill. The answer is that it is very easy to create your own class if you need to. It is unlikely that you will need to think about types or modes at a beginner or intermediate level, and I'm guessing there are many advanced users out there who are a whizz at writing and maintaining classes, but know (or care) little about the underlying types and modes.
Upvotes: 2