How Do You Find the Data Type in R?


Angela Bailey

When working with data in R, it is essential to know the data type of each variable or object. The data type determines how the values of a variable can be stored and manipulated. In this tutorial, we will explore different methods to find the data type in R.

Using the class() Function

To find the data type of an object in R, you can use the class() function. This function returns the class or data type of an object.

# Example 1: Finding the Data Type of a Numeric Variable
num_var <- 10

The above code will output "numeric", indicating that the variable num_var is of numeric data type.

You can also use the class() function directly with a value:

# Example 2: Finding the Data Type of a Value

The above code will output "numeric", as 3.14 is a numeric value.

Using typeof() Function

In addition to class(), you can also use the typeof() function to determine the internal representation or storage mode of an object. The typeof() function returns a character string indicating the storage mode.

# Example 3: Using typeof() to Find Data Type
char_var <- "Hello"

The above code will output "character", indicating that char_var is a character variable.

Using str() Function

The str() function is another useful tool for examining the structure of an object. It provides a compact display of the internal structure of an R object. The output includes the class, dimension, and other relevant information.

# Example 4: Using str() to Examine Object Structure
vec <- c(1, 2, 3)

The above code will output:

  • "num" [1:3] 1 2 3

This indicates that vec is a numeric vector with values 1, 2, and 3.


In this tutorial, we explored various methods to find the data type in R. The class(), typeof(), and str() functions provide different ways to determine the data type or structure of an object. By using these functions, you can gain better insights into your data and ensure that you are performing appropriate operations on them.

Remember to always check the data type before applying any operations or transformations to avoid errors and unexpected results.

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

Privacy Policy