Demystifying Data Types in R: A Beginner’s Guide with Code Examples

[This article was first published on Steve's Data Tips and Tricks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.


Ever wondered what kind of information your data holds in R? Knowing the data type is crucial for performing the right analysis and avoiding errors. This post will equip you with the skills to check data types in R, making your coding journey smoother and more efficient.

Unveiling the Data Within: Common Data Types in R

Imagine your data as a diverse collection of individuals. Some might be numbers (like age or weight), while others might be text (like names or addresses). These different categories are called data types, and R recognizes several key ones:

  • Numeric: Numbers, including integers (whole numbers) and decimals.
  • Character: Text strings, like words or sentences.
  • Logical: True or False values.
  • Factor: Categorical data with defined levels (e.g., colors: red, green, blue).

Unveiling the Secrets: Checking Data Types with Ease

Now, let’s equip ourselves with the tools to identify these data types in our R projects. Here are two powerful functions:

  • class(): This function provides a general overview of the data type, like “numeric” or “character.”
  • typeof(): This function delves deeper, revealing more specific details within the data type (e.g., “double” for decimals within “numeric”).

Putting Knowledge into Action: Code Examples

Ready to see these functions in action? Let’s explore some examples:

Example 1: Checking the type of a single variable:

# Create a variable with different data types
age <- 25
name <- "Alice"
is_employed <- TRUE

# Check the data types using class()
class(age)  # Output: "numeric"
[1] "numeric"
class(name) # Output: "character"
[1] "character"
class(is_employed) # Output: "logical"
[1] "logical"
# Check for even more details using typeof()
typeof(age)  # Output: "double"
[1] "double"
typeof(name) # Output: "character"
[1] "character"
typeof(is_employed) # Output: "logical"
[1] "logical"

Example 2: Examining data types within a data frame:*

# Create a sample data frame
data <- data.frame(
  ID = 1:5,
  Name = c("Bob", "Charlie", "David", "Emily", "Fiona"),
  Age = c(28, 32, 41, 25, 37)

# Peek into the data frame's structure using str()
'data.frame':   5 obs. of  3 variables:
 $ ID  : int  1 2 3 4 5
 $ Name: chr  "Bob" "Charlie" "David" "Emily" ...
 $ Age : num  28 32 41 25 37

The str() function displays a detailed summary of the data frame, including the names and data types of each column.

Time to Practice!

Now it’s your turn to explore! Try creating your own variables and data frames with different data types. Use class() and typeof() to unveil their hidden identities. Remember, practice makes perfect, and the more you experiment, the more comfortable you’ll become with data types in R.

Bonus Challenge: Can you think of situations where knowing the data type is crucial for your analysis? Share your thoughts in the comments below!

By understanding and effectively checking data types, you’ll be well on your way to mastering data manipulation and analysis in R. So, keep exploring, keep coding, and keep demystifying your data!

To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)