How to Check the Object Type in R

To check the object type in R using the class() function:

(1) Numeric:

x <- 7
class(x)

Output:

numeric

(2) Character:

y <- "Hello world"
class(y)

Output:

character

(3) Logical:

z <- TRUE
class(z)

Output:

logical

(4) Date:

d <- as.Date("2024-02-16")
class(d)

Output:

Date

(5) Integer:

i <- as.integer(7)
class(i)

Output:

integer

(6) Factor:

f <- factor(c("A", "B", "C", "D", "E"))
class(f)

Output:

factor

(7) Matrix:

m <- matrix(1:6, nrow = 2, ncol = 3)
class(m)

Output:

matrix

(8) DataFrame:

df <- data.frame(x = 1:3, y = c("a", "b", "c")))))
class(df)

Output:

data.frame

Leave a Comment