Welcome to our next lesson. In our previous session, we explored R's fundamental data types—numeric, character, and logical—and saw how R handles individual pieces of information. However, a systematic review involves data from multiple studies, not just single values. For instance, you'll have a list of pain scores from each study's treatment group, a collection of author names, or a series of publication years.
This lesson builds directly on that foundation by introducing vectors, R's primary tool for storing an ordered sequence of values. You will learn how to create vectors using the c() function and, crucially, how to select and retrieve specific elements from them using bracket notation ([]). Mastering vectors is a major step toward organizing the data extracted from your primary studies into a format R can analyze.
What is a Vector?
In R, a vector is the simplest type of data structure. Think of it as a single column in a spreadsheet—a list of items that are all of the same type. For example, you can have a vector of numbers (like participant counts for several studies), a vector of characters (like the study IDs), or a vector of logicals (indicating which studies were randomized).
The most important rule for vectors is that all elements in a single vector must be of the same data type. If you try to mix types, R will force them all into the most flexible type in a process called coercion, which we touched on in the last lesson.
Creating Vectors with c()
To create a vector, you use the c() function, which stands for "combine" or "concatenate". You simply list the elements you want in the vector inside the parentheses, separated by commas.
Let's create a few vectors relevant to rehabilitation research. You can type these directly into your R script or console.
# A numeric vector of mean age from three studies
mean_age <- c(45.2, 51.5, 48.9)
# A character vector of the interventions used
interventions <- c("strengthening", "stretching", "control")
# A logical vector indicating if allocation was concealed
concealed <- c(TRUE, TRUE, FALSE)
After running this code, inspect your Environment pane in RStudio. You'll see these three new objects. Notice how R indicates their type (e.g., num, chr, logi) and shows the first few elements.
The following short video provides a clear, practical demonstration of creating numeric and character vectors, and it also revisits the important concept of coercion when you try to combine different data types.
This video, "Introduction to R: Vectors," begins with a concise explanation of how to create vectors using the c() function and what happens when you combine vectors of different types.
Please watch the first part of the video, from the beginning up to the timestamp where vectors are created. Pay attention to how combining a numeric vector and a character vector forces the numbers to become characters.
Accessing Vector Elements: Subsetting with []
Once you have a vector, your next task is to access its contents. You might want to retrieve the value from a single study, a range of studies, or a specific selection of studies. This process is called subsetting, and in R, it's done using square brackets [].
The basic syntax is vector_name[index], where the index specifies which element(s) you want. It's essential to know that R is a 1-indexed language, meaning the first element is at index 1, the second at index 2, and so on.

There are three primary ways to specify an index to subset a vector:
- By numeric position: To get a specific element or a range.
- By exclusion: To get everything except certain elements.
- By logical condition: To get elements that meet a specific criterion.
The following comprehensive reading from a biomedical data science textbook will walk you through creating and subsetting vectors. It provides clear code examples that are highly relevant to your goals.
Chapter 1 Introduction to R | Biomedical Data Science - introduction with case studies
This chapter excerpt from "Biomedical Data Science" provides a thorough introduction to vectors, from creation and coercion to the various methods of subsetting.
Please read the following sections: First, read the section Vector, which covers creating vectors with c() and explains the hierarchy of coercion (logical < integer < numeric < character). Next, read the subsection Index. This is the core of our lesson. It demonstrates how to use numeric indices to retrieve a single element (e.g., scores[5]), a range of elements (scores[2:5]), a specific collection (scores[c(2,5,7)]), and how to exclude elements (scores[-5]).
The Power of Logical Subsetting
The most powerful and common subsetting method in data analysis is using a logical vector. This technique is fundamental for filtering your data based on specific criteria.
Here’s how it works: you create a logical vector of TRUE and FALSE values that is the same length as your data vector. When you use this logical vector as your index, R returns only the values from the data vector that correspond to a TRUE position.

Let's imagine you have a vector of sample sizes from studies in your review and you only want to analyze those with more than 50 participants.
# Sample sizes from five studies
n_participants <- c(35, 62, 104, 48, 88)
# 1. Create a logical vector based on a condition
is_large_study <- n_participants > 50
# See the result of the logical comparison
print(is_large_study)
# [1] FALSE TRUE TRUE FALSE TRUE
# 2. Use the logical vector to subset the original data
large_studies_n <- n_participants[is_large_study]
# See the final filtered vector
print(large_studies_n)
# [1] 62 104 88
This two-step process—creating a logical condition and then using it to subset—is a pattern you will use constantly in R. The video you started earlier has an excellent segment explaining exactly this.
Let's return to the "Introduction to R: Vectors" video to see these subsetting techniques in action.
Please watch the following segments: First, watch the section on accessing elements by index. This covers using single numbers, ranges (:), and vectors (c()) inside the square brackets. Next, see how to exclude elements using the minus sign. Finally, pay close attention to the section on logical indexing. This is the most important subsetting method for data analysis, and the video provides a great walkthrough.
Conclusion
In this lesson, you've learned how to manage collections of data, moving from single variables to vectors. This is the first major step in building a dataset for your meta-analysis in R.
Here are the key takeaways:
- Vectors are ordered collections of elements of the same data type.
- The
c()function is used to create vectors (e.g.,my_vector <- c(1, 2, 3)). - If you mix data types in a vector, R uses coercion to make them all the same type.
- You subset vectors using square brackets
[]and an index. R is 1-indexed. - Subsetting can be done with a single number (
vec[1]), a range (vec[2:5]), a vector of indices (vec[c(1,5)]), or by exclusion (vec[-1]). - Logical subsetting (
vec[vec > 50]) is a powerful and efficient way to filter data based on conditions.
So far, we have been using the functions that come with R by default. However, R's true power comes from its vast ecosystem of add-on packages. In our next lesson, we will learn how to find, install, and load R packages from the Comprehensive R Archive Network (CRAN). This will equip us with the tools we need for more complex tasks, including eventually installing the meta package for our meta-analyses.