How Do You Check Data Type in R?

//

Angela Bailey

In R, checking the data type of a variable is an essential task for data analysis and manipulation. By knowing the data type, you can perform appropriate operations and avoid unexpected errors. In this tutorial, we will explore various ways to check the data type in R.

typeof() function

The typeof() function is a handy built-in function in R that returns the data type of an object. It is straightforward to use and provides a quick way to check the data type.

Here’s an example:

num_var <- 10
typeof(num_var)

The output will be:

[1] "double"

This indicates that the variable num_var is of numeric double data type.

class() function

Another useful function to determine the data type is class(). The class() function provides more detailed information about the specific class or classes to which an object belongs.

To illustrate this, consider the following example:

char_var <- "Hello"
class(char_var)
[1] "character"

This tells us that the variable char_var is of class "character".

dplyr package's glimpse() function

If you are working with large datasets or multiple variables, it can be time-consuming to check each variable one by one. The glimpse() function from the dplyr package provides a concise summary of the dataset, including the data types of each variable.

library(dplyr)

df <- data.frame(id = c(1, 2, 3),
                 name = c("John", "Jane", "Mike"),
                 age = c(25, 30, 35))

glimpse(df)
Rows: 3
Columns: 3
$ id   <dbl> 1, 2, 3
$ name <chr> "John", "Jane", "Mike"
$ age  <dbl> 25, 30, 35

From the output, we can see that the id variable is of type "dbl", the name variable is of type "chr", and the age variable is also of type "dbl".

R's built-in is.*() functions

R provides a set of built-in functions that start with "is.". These functions allow you to check if an object belongs to a specific data type. For example, is.numeric(), is.character(), and so on.

To demonstrate this, consider the following example:

x <- c(1, 2, "three", TRUE)

is.numeric(x)
is.character(x)
is.logical(x)
[1] FALSE
[1] FALSE
[1] FALSE

From the output, we can observe that the object x is not of numeric, character, or logical data type. It contains a mix of different data types.

Conclusion

In this tutorial, we explored various methods to check the data type in R. The typeof() function provides a quick way to determine the data type of an object. The class() function gives more detailed information about the specific class or classes to which an object belongs.

The glimpse() function from the dplyr package provides a summary of the dataset with data types for each variable. Lastly, R's built-in functions starting with "is. " can be used to check if an object belongs to a specific data type.

By understanding and using these techniques effectively, you will have better control over your data analysis and ensure appropriate operations on variables based on their respective data types.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy