3 Easy Ways to Check Data type in R

Several useful functions in R check the variable type or convert it between different variable types. For most of the major R objects, language provides is.datatype() function or as.datatype() function.

For example, if we are checking for the vectors, then the R language provides is.vector() function to check if it is a vector or not and as.vector() function to coerce the different data types.

Objects are the instance of the class, and everything in R is an object. To know more about this, look at Data types in R.

These functions are R objects specific and not general, and there are functions through which you can find the data type of variables.

How to check data type in R

There are 3 main ways to check the data type of a variable in R.

  1. Using the class() function: It returns the data type of any R object.
  2. Using the typeof() function: It returns the data type of a variable.
  3. Using the str() function: It returns the data type of every variable in a data frame.

Method 1: Using a class() function

To check the data type of any R object, use the class() function. If the object does not have a class attribute, it has an implicit class, prominently “matrix“, “array“, “function”, or “numeric,” or the result of typeof(x).

Syntax

class(x)

The is an R Object.

Example

rv <- c(11, 15, 18, 19, 21)
rv
class(rv)

Output

[1] 11 15 18 19 21
[1] "numeric"

Method 2: Using the typeof() function

To get the data type of a variable in R, use the typeof() function. The typeof() is a built-in function that defines any R object’s (internal) type or storage mode.

Syntax

typeof(x)

The is any R object.

Example

Define a vector of integers and see its data type using the typeof() function.

rv <- c(11, 15, 18, 19, 21)
rv
typeof(rv)

Output

[1] 11 15 18 19 21
[1] "double"

The internal data type of the above vector is numeric or double.

To check the numeric data type in R programming, use the is.numeric() function.

rv <- c(11, 15, 18, 19, 21)
rv
is.numeric(rv)

Output

[1] 11 15 18 19 21
[1] TRUE

And it returns TRUE, meaning the vector contains numeric components.

If the vector contains the character components, then it will return FALSE.

rv <- c("Check", "R", "Data type")
rv
is.numeric(rv)

Output

[1] "Check" "R" "Data type"
[1] FALSE

To check the specific character data type in R, use the is.character() method.

rv <- c("Check", "R", "Data type")
rv
is.character(rv)

Output

[1] "Check" "R" "Data type"
[1] TRUE

To check the data type of the list in R, use the typeof( ) function.

rl <- list("Check", "R", "Data type")
typeof(rl)

Output

[1] "list"

Method 3: Using the str() function

To check the data type of every variable in a data frame, use the str() function in R.

df <- data.frame(a1 = 1:3, a2 = 4:6, a3 = 7:9)

str(df)

Output

'data.frame':    3 obs. of 3 variables:
$ a1: int   1 2 3
$ a2: int   4 5 6
$ a3: int   7 8 9

You can see that:

  1. Variable a1 is a numeric variable.
  2. Variable a2 is a numeric variable.
  3. Variably a3 is a numeric variable.

FAQ

How to check the mode of a variable in R?

Use the typeof() function to check the internal mode of a variable in R.

How to check if a variable is a list or not in R?

Use the is.list() function to check if a variable is a list in R.

How to check if a variable is a dataframe or not in R?

Use the is.data.frame() function to check if a variable is a data frame in R.

How to check if a variable is a specific data type in R?

You can use the is. functions, such as is.numeric(), is.character(), is.integer(), is.logical() to check if a variable has a specific data type in R.

Conclusion

The easy way to check a variable’s data type is to use either class() or typeof() function in R.

If you want to check the specific data type, R also provides those functions, like is.numeric() or is.character(). They return either a TRUE or FALSE value.

Leave a Comment