Check Variable Data Types in R

  1. Check Variable Data Types in R
  2. Check Data Type of One Variable in R
  3. Check Data Type of Every Variable in Data Frame in R
  4. Check If a Variable is a Specific Data Type in R

There are different methods to check data types of variables for different data types. This tutorial demonstrates how to check the data type of variables in R.

Check Variable Data Types in R

Many methods in R can be used to check the data types of variables. These methods are given below.

# for checking data type of one variable
typeof(x)
class(x)

# for checking the data type of every variable in a data frame
str(dataframe)

# for checking if a variable is a specific data type
is.factor(x)
is.numeric(x)
is.logical(x)
is.complex(x)
is.integer()

Let’s try each method one by one.

Check Data Type of One Variable in R

There are two methods to check the data type of a single variable or object, the typeof() method and the class() method. Both the methods take one parameter, which is the variable or object.

Let’s try one example for both methods.

#define variable Demo
Demo <- c("Delftstack1", "Delftstack2", "Delftstack3", "Delftstack4", "Delftstack5", "Delftstack6")

#check the data type of Demo using typeof method
print('The output for typeof method:')
typeof(Demo)

#check the data type of Demo using the class method
print('The output for class method:')
class(Demo)

The code above will check the variable type with the typeof() method and then with the class() method. See output:

[1] "The output for typeof method:"
[1] "character"

[1] "The output for class method:"
[1] "character"

Check Data Type of Every Variable in Data Frame in R

The str() method is used to check the data type of each variable in a data frame. It takes one parameter, which is the data frame.

See example:

#create a data frame
Delftstack <- data.frame(Name=c('Jack', 'John', 'Mike', 'Michelle', 'Jhonny'),
                         LastName=c('Danials', 'Cena', 'Chandler', 'McCool', 'Nitro'),
                         Id=c(101, 102, 103, 104, 105),
                         Designation=c('CEO', 'Project Manager', 'Senior Dev', 'Junior Dev', 'Intern'))

#View the dataframe
Delftstack

#Check the data type of every variable in dataframe
str(Delftstack)

The code above checks the data type for each variable in the data frame. See output:

      Name LastName  Id     Designation
1     Jack  Danials 101             CEO
2     John     Cena 102 Project Manager
3     Mike Chandler 103      Senior Dev
4 Michelle   McCool 104      Junior Dev
5   Jhonny    Nitro 105          Intern

'data.frame':	5 obs. of  4 variables:
 $ Name       : chr  "Jack" "John" "Mike" "Michelle" ...
 $ LastName   : chr  "Danials" "Cena" "Chandler" "McCool" ...
 $ Id         : num  101 102 103 104 105
 $ Designation: chr  "CEO" "Project Manager" "Senior Dev" "Junior Dev" ...

Check If a Variable is a Specific Data Type in R

There are a few methods to check if a variable is of a specific type, one method for each data type. Each method takes the variable as a parameter and returns True or False.

Let’s try an example to show each method.

#create variables
a <- 3

b <- 5.3

c <- "Delftstack"

d <- TRUE

e <- factor(c('A', 'B', 'C', 'D'))

i <- as.integer(a)


## Check types of variables

print(is.numeric(a))

print(is.complex(b))

print(is.character(c))

print(is.logical(d))

print(is.factor(e))

print(is.integer(i))

The code above creates a variable for each type and checks if it has the corresponding data type. See output:

> ## Check types of variables

[1] TRUE

[1] FALSE

[1] TRUE

[1] TRUE

[1] TRUE

[1] TRUE

The code shows that variable b is not a complex type variable; all the other variables have the same data type as the method.

We can also use these methods to check the data types for the columns of a data frame. See example:

#create a data frame
Delftstack <- data.frame(Name=c('Jack', 'John', 'Mike', 'Michelle', 'Jhonny'),
                         LastName=c('Danials', 'Cena', 'Chandler', 'McCool', 'Nitro'),
                         Id=c(101, 102, 103, 104, 105),
                         Designation=c('CEO', 'Project Manager', 'Senior Dev', 'Junior Dev', 'Intern'))


#check if Name column is character
is.character(Delftstack$Name)

#check if LastName column is complex
is.complex(Delftstack$LastName)

#check if the Id column is numeric
is.numeric(Delftstack$Id)

The code above checks if a column of the data frame is a specific type of data or not. See output:

> #check if Name column is character
[1] TRUE

> #check if LastName column is complex
[1] FALSE

> #check if Id column is numeric
[1] TRUE

Related Article - R Variable

  • Create and Interpret Dummy Variables in R