R – type of

To get type of a value or variable or object in R programming, call typeof() function and pass the value/variable to it.

In this tutorial, we will learn how to use typeof() function to find the type of a value, with examples.

Syntax

The syntax to use typeof() function to get the data type of variable x is

</>
Copy
typeof(x)

The function returns a character string.

The following are some of the possible values that typeof() function return.

  • “logical”
  • “integer”
  • “double”
  • “complex”
  • “character”
  • “raw”
  • “list”
  • “NULL”
  • “closure”
  • “special”
  • “builtin”
  • “environment”
  • “S4”

Examples

In the following program, we will initialize a variable x with a value of 2.8, and find its type using typeof() function.

example.R

</>
Copy
x <- 2.8
typeof(x)

Output

[1] "double"

Now, let us assign x with TRUE, and find the type of x programmatically.

example.R

</>
Copy
x <- TRUE
typeof(x)

Output

[1] "logical"

Conclusion

In this R Tutorial, we learned how to get the type of an object, using typeof() function, with the help of examples.