Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 5

Understanding Data Types: Numeric, Character, and Logical

Recommended resources

In our last lesson, we focused on assigning numerical values to variables and performing calculations. This is a crucial first step, but the data you'll work with in your systematic reviews is more varied than just numbers. You'll encounter study authors, intervention types, and logical flags indicating if a study meets inclusion criteria. R needs a way to understand and differentiate these various kinds of information.

This lesson introduces the concept of data types. You will learn to distinguish between the three most common data types you'll encounter in R: numeric, character, and logical. Understanding these types is fundamental, as R's behavior changes depending on the type of data it's handling. This knowledge will prevent common errors and forms the basis for structuring your meta-analysis data correctly.

More Than Just Numbers: An Overview of Data Types

In the real world, data comes in many forms. In your field of physical therapy research, you might record a patient's pain score (a number), the name of their prescribed exercise (text), and whether they completed the program (a yes/no answer). R uses data types to represent this diversity.

While R has several data types, we will focus on three essential ones for now:

  1. Numeric: Represents numbers. This can include integers (like 30 participants) and numbers with decimal points (like a mean difference of 4.25). In R, these are generally categorized as numeric or, more specifically, double (for double-precision floating-point numbers) and integer. For our purposes, we'll mostly refer to them as numeric.
  2. Character: Represents text, also known as "strings". This includes things like study IDs ("Smith 2021"), author names, or intervention descriptions ("strength training"). Character data is always enclosed in quotation marks.
  3. Logical: Represents the boolean values TRUE or FALSE. This is perfect for binary information, such as whether a study is randomized (is_randomized <- TRUE) or if it meets an inclusion criterion.

The following image provides a clear visual summary of these data types and how R identifies them.

This diagram illustrates four fundamental R data types: `double` (which has the class `numeric`), `integer`, `logical`, and `character`. For each type, it shows an example of how to create a collection of such data using `c()` and the output returned by the `class()` and `typeof()` functions.

This diagram illustrates four fundamental R data types: double (which has the class numeric), integer, logical, and character. For each type, it shows an example of how to create a collection of such data using c() and the output returned by the class() and typeof() functions.

As you can see, R has functions like class() and typeof() to help you inspect what kind of data you're working with.


Working with Data Types in R

Let's explore these types with some practical examples relevant to a systematic review. Open a new R script in RStudio and try running these lines of code.

# --- Numeric Data ---
# A mean pain score from a study (0-10 scale)
pain_score_mean <- 7.3

# The number of participants in the control group
n_control <- 52

# --- Character Data ---
# The author and year of a study
study_id <- "Miller et al. 2019"

# The type of outcome measure used
outcome_measure <- "6-minute walk test"

# --- Logical Data ---
# Was the study a randomized controlled trial?
is_rct <- TRUE

# Did the study report a standard deviation?
reported_sd <- FALSE

After you run these lines, look at your Environment pane in RStudio. You'll see each variable listed along with its value. Notice how R displays character data with quotes, while numerics and logicals appear without them.

A critical rule to remember is that you cannot perform mathematical operations on character data, even if it looks like a number.

# This will work
total_participants <- 52 + 55 
print(total_participants)

# This will cause an error
# total_participants_error <- "52" + "55" 
# Error in "52" + "55" : non-numeric argument to binary operator

R sees "52" as text, not a number, and doesn't know how to "add" two pieces of text together with the + operator. This is a very common source of errors for beginners.


Checking and Converting Types

Since data types are so important, R provides functions to check and even change them. This process of changing a variable's type is called coercion.

The following short video from DataCamp provides a concise and clear demonstration of R's basic data types and how to work with them.

R tutorial: The basic data types in R

This video, "The basic data types in R," clearly explains and demonstrates the logical, numeric, integer, and character types. It also introduces the crucial concept of type conversion (coercion).

Please watch the entire video. Pay close attention to: The different data types and how they are written in R: TRUE/FALSE for logical, numbers for numeric, and text in quotes for character. The distinction between a numeric (with decimals) and an integer (whole number, specified with L) from this segment. How to convert, or coerce, data from one type to another using as. functions, and what happens when a conversion isn't possible (coercion examples). The concept of NA (Not Available) for a failed conversion is very important.

As the video showed, the three most important functions for managing data types are:

  • class(variable): Tells you the data type of a variable.
  • is.numeric(variable), is.character(variable), etc.: Returns TRUE or FALSE to confirm if a variable is of a specific type.
  • as.numeric(variable), as.character(variable), etc.: Attempts to convert a variable to a specific type.

To deepen your understanding, let's explore some more practical examples in a reading.

R Data types - What kind of data do I have?

This article from "R for ecology" gives clear explanations and useful code examples for numeric, character, and logical data types. The principles are universal, even though the examples are from a different field.

Please read the following three sections of the article: Start at the heading "Numeric" and read to the end of that section. Continue with the section "Character". Note the example showing that you cannot calculate the mean() of numbers stored as characters. Finally, read the section "Logical". Pay special attention to the part that shows how you can do math with logical values. For example, using sum() on a logical vector counts the number of TRUEs, which is an extremely useful technique.

The ability to use sum() on a logical vector is a powerful trick. Imagine you have a variable for each study indicating if it met your inclusion criteria (e.g., met_criteria <- c(TRUE, TRUE, FALSE, TRUE)). You could simply run sum(met_criteria) to find out that 3 studies were included.


Conclusion

In this lesson, you have moved beyond simply storing numbers to understanding how R represents different kinds of information. This is a foundational concept that underpins all data handling and analysis in R.

Here are the key takeaways:

  • R uses data types to distinguish between different kinds of information.
  • The three most common data types for analysis are numeric (for numbers), character (for text in quotes), and logical (for TRUE or FALSE).
  • The type of data determines what operations you can perform on it; for instance, you cannot do arithmetic on character data.
  • You can use class() to check a variable's type and as.*() functions to convert it, being mindful that impossible conversions result in NA.

So far, we have only worked with single values stored in variables. In your meta-analysis, you will have data from many studies. In our next lesson, we will learn about vectors, which are R's way of storing a sequence of multiple values of the same data type. This will be our first step toward building a complete dataset for your analysis.