Open In App

Check Data Type of each DataFrame Column in R

Last Updated : 21 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to identify the data type of variables in a column of a given dataframe using R Programming language. We will be using str() and sapply() function in this article to check the data type of each column in a dataframe. 

Method 1: Using str() function

str() function in R Language is used for compactly displaying the internal structure of an R object. It can display even the internal structure of large lists which are nested. It provides one-liner output for the basic R objects letting the user know about the object and its constituents. 

Example 1: In the code below, we are passing a dataframe as an argument in the str() function as we want to check its datatype. Notice the “name” column is identified as Factor, roll column is identified as num or numeric, date column is identified as Date(as we have used as.Date() function to convert it to the Date data type), pwd column is identified as logi or Logic data type.

Syntax: str(object, …)

Parameter: object: Any R object about which information is required.

Code:

R




# sample dataframe
df <- data.frame(name = c("Welcome", "to", "Geeks", "for", "Geeks"),
                 roll = c(10, 40.1, 50.5, 80, 70),
                 date = as.Date(c("2001-10-30","2000-01-21",
                                  "1999-02-16","1950-12-03"
                                  ,"1995-11-09")),
                   
                 pwd = c(FALSE, TRUE, TRUE, TRUE, FALSE)
                 )
# checks the data type of each column
str(df)


Output:

'data.frame':    5 obs. of  4 variables:
 $ name: Factor w/ 4 levels "for","Geeks",..: 4 3 2 1 2
 $ roll: num  10 40.1 50.5 80 70
 $ date: Date, format: "2001-10-30" "2000-01-21" ...
 $ pwd : logi  FALSE TRUE TRUE TRUE FALSE

Example 2:

Notice in the previous example “name” column is identified as Factor data type. We can pass “stringAsFactors=FALSE” as an argument while defining a dataframe if you want the “name” column to be identified as chr or character datatype.

Syntax:

stringAsFactors= FALSE

R




# sample dataframe
df <- data.frame(name = c("Welcome", "to", "Geeks", "for", "Geeks"),
                 roll = c(10, 40.1, 50.5, 80, 70),
                 date = as.Date(c("2001-10-30","2000-01-21","1999-02-16",
                                  "1950-12-03","1995-11-09")),
                 pwd = c(FALSE, TRUE, TRUE, TRUE, FALSE), 
                 stringsAsFactors = FALSE
                 )
  
# checks the data type of each column
str(df)


Output:

'data.frame':    5 obs. of  4 variables:
 $ name: chr  "Welcome" "to" "Geeks" "for" ...
 $ roll: num  10 40.1 50.5 80 70
 $ date: Date, format: "2001-10-30" "2000-01-21" ...
 $ pwd : logi  FALSE TRUE TRUE TRUE FALSE

Method 2: Using sapply() and class() function

sapply() function in R Language takes a list, vector, or data frame as input and gives output in vector or matrix. It is useful for operations on list objects and returns a list object of the same length as the original set.

Syntax: sapply(X, FUN)

Parameters:

X: A vector or an object

FUN: Function applied to each element of x

The sapply() function takes an object(dataframe “df” in this case) and a function(class() in this case) as arguments, and it applies the function to each element of the object(dataframe in this case). In the code below, we have used sapply() function to apply class() function to each column of the dataframe, and the class() function returns the data type of the column.

Code:

R




# sample dataframe
df <- data.frame(name = c("Welcome", "to", "Geeks", "for", "Geeks"),
                 roll = c(10, 40.1, 50.5, 80, 70),
                 date = as.Date(c("2001-10-30","2000-01-21","1999-02-16",
                                  "1950-12-03","1995-11-09")),
                 pwd = c(FALSE, TRUE, TRUE, TRUE, FALSE), 
                 stringsAsFactors = FALSE
                 )
  
# checks the data type of each column
sapply(df,class)


Output:

 name        roll        date         pwd 
"character"   "numeric"      "Date"   "logical" 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads