Check the Data Type of each DataFrame Column in R

You may use str() in order to check the data type of each DataFrame column in R:

str(dataframe name)

Next, I’ll review a simple example with the steps to:

  • Create a DataFrame in R; and
  • Check the data type of each column in the DataFrame

Steps to Check the Data Type of each DataFrame Column in R

Step 1: Create a DataFrame

To begin, create your DataFrame in R.

For example, I created the following DataFrame about 5 individuals:

df <- data.frame(name = c("Jon", "Bill", "Maria", "Ben", "Emma"),
                 age = c(23, 41, 32, 57, 38),
                 date_of_birth = as.Date(c("1997-05-21","1979-03-15","1988-11-08","1963-02-23","1982-09-12")),
                 employed = c(TRUE, FALSE, TRUE, TRUE, FALSE)
                 )
print(df)

This is how the DataFrame would look like once you run the code in R:

DataFrame in R

Step 2: Check the Data Type of each Column

Recall that you may use str() in order to check the data type of each column in your DataFrame:

str(dataframe name)

In our example, the DataFrame name is df.

Therefore, the complete code to check the data type for each column is:

df <- data.frame(name = c("Jon", "Bill", "Maria", "Ben", "Emma"),
                 age = c(23, 41, 32, 57, 38),
                 date_of_birth = as.Date(c("1997-05-21","1979-03-15","1988-11-08","1963-02-23","1982-09-12")),
                 employed = c(TRUE, FALSE, TRUE, TRUE, FALSE)
                 )
str(df)

You’ll now see the data type that corresponds to each column in the DataFrame:

Check the Data Type of each DataFrame Column in R

Notice that the ‘name‘ column is represented as a Factor. You may then add the syntax of stringsAsFactors = FALSE to the DataFrame in order to represent that column as a character:

stringsAsFactors = FALSE

So the complete R code would look like this:

df <- data.frame(name = c("Jon", "Bill", "Maria", "Ben", "Emma"),
                 age = c(23, 41, 32, 57, 38),
                 date_of_birth = as.Date(c("1997-05-21","1979-03-15","1988-11-08","1963-02-23","1982-09-12")),
                 employed = c(TRUE, FALSE, TRUE, TRUE, FALSE), stringsAsFactors = FALSE
                 )
str(df)

You’ll now notice that the name column is represented as a character:

Check the Data Type of each DataFrame Column in R