What is the class() function in R?
Overview
The class() function in R is used to return the values of the class attribute of an R object.
Syntax
Following is the syntax for the method:
class(x)
Parameters
The class() function takes the parameter value:
x: This represents the R object whose class attribute is to be determined.
Return value
The class() function returns the class attribute of an R object.
Example
Let’s look at the code below:
# creating R objectsmydate <- as.Date('2015-03-12')myfunction <- function(x) { x*x}myname <- "Theo"mydf <- data.frame(c1=1:2, c2=letters[1:2])# getting their class attributes using the class() functionclass(mydate)class(myfunction)class(myname)class(mydf)
Explanation
In the code above:
- Lines 2 to 5: We create different R objects.
- Lines 9 to 12: We obtain the class attributes of the R objects we created using the
class()function.