8  Data Types

8.1 Questions

  • What are the primary data types in R?

  • Can one convert between different data types?

  • How does R handle different types of data?

  • How can I determine variable type in R?

  • What are some helpful functions for identifying and manipulating data types?

  • How do data types impact my analysis and coding in R?

8.2 Learning Objectives

  • Identify and differentiate between common data types in R.

  • Learn how to convert between different data types.

  • Use functions like class(), typeof(), and str() to inspect types.

  • Convert between data types using the as.___() functions.

8.3 Lesson Content

8.3.1 Introduction

R and RStudio utilize multiple data types to store different kinds of data.

The most common data types in R are listed below.

Data Type Description
Numeric The most common data type. The values can be numbers or decimals (all real numbers).
Integer Special case of numeric data without decimals.
Logical Boolean data type with only 2 values (TRUE or FALSE).
Complex Specifies imaginary values in R.
Character Assigns a character or string to a variable. The character variables are enclosed in single quotes (‘character’) while the string variables are enclosed in double quotes (“string”).
Factor Special type of character variable that represents a categorical such as gender.
Raw Specifies values as raw bytes. It uses built-in functions to convert between raw and character (charToRaw() or rawToChar()).
Dates Specifies the date variable. Date stores a date and POSIXct stores a date and time. The output is indicated as the number of days (Date) or number of seconds (POSIXct) since 01/01/1970.

8.3.2 Examples of data types in R

  1. Numeric
89.98
55
  1. Integer
5L
5768L
  1. Logical
TRUE
FALSE
  1. Complex
10 + 30i
287 + 34i
  1. Character or String
'abc'
"def"
"I like learning R"

NOTE: A comprehensive overview of handling characters with the stringr package will be provided in a subsequent series on the Tidyverse.

  1. Dates
"2022-06-23 14:39:21 EAT"
"2022-06-23"
  1. Raw
  1. Factor

8.3.3 Inspecting various data types

Several functions exist to examine the features of the various data types. These include:

1. typeof() – what is the data type of the object (low-level)?

2. class() – what is the data type of the object (high-level)?

3. length() – how long is the object?

4. attributes() – any metadata available?

Let’s look at how these functions work with a few examples:

a <- 45.84
b <- 858L
c <- TRUE
d <- 89 + 34i
e <- 'abc'
  1. Examine the data type at a low-level with typeof()
  1. Examine the data type at a high-level with class()
  1. Use the is.____() functions to determine the data type. To test whether the variable is of a specific type, we can use the is.____() functions.

First, we test the variable a which is numeric.

Second, we test the variable c which is logical.

8.3.4 Converting between various data types

To convert between data types, we can use the as.___() functions. These include: as.Date(), as.numeric(), and as.factor(). Additionally, other helpful functions include factor() which adds levels to the data and nchar() which provides the length of the data.

8.3.4.1 Implicit vs. Explicit conversion

Examples

Order of precedence when converting between data types is:

Character > Numeric > Integer > Logical

8.4 Exercises

  • What are the basic data types in R? Give examples of each.

  • How can you check the data type of a variable in R?

  • Use the class(), typeof(), and str() functions to check the data type of objects.

  • Describe the process of coercion in R.

  • Provide an example of implicit and explicit coercion between data types.

  • How can you coerce or convert between different data types in R?

  • What does it mean for an object in R to have attributes? What attributes are commonly used?

8.5 Summary

In this chapter, I have demonstrated the primary data types in R. Additionally, the learner has been trained on how to identify and convert between the different data types. Next, we will review vectors, which are key data structures in R/RStudio.