Module 2: Organizing the Subject Pool
(Data Structures).
in psychological research we deal with multiple participants, thousands of trials, and multiple variables. To
handle this we must group out atomic types into Data Structures.
Atomic data types (Numeric, Character Logical) are the individual bricks, Data Structures are the walls,
rooms and buildings you construct with them. R has a specific architecture for how it groups data together
based in two rules:
Dimensions: 1D or 2D and,
Homogeneity: Does every item have to be the exact same type of data?
1. Vectors: The Foundation of R(c())
The most fundamental data structure in R is the vector. A vector is a one-dimensional array that holds a
sequence of data elements.
In R, a vector must be homogeneous it can only one type of atomic data. You can have a vector of
500 reaction times (Numeric), or a vector of 50 subjects IDs (character), but you can not mix text and
numbers in the same vector. If you try, R will silently force them all into characters → This is called
coercion and ruins statistical models
The Combine Function (c()): To create a vector, you wrap your values in c(), which stand for
“combine” or “concatenate”.
Example:
# A numeric vector of Beck Depression Inventory (BDI) scores for 4 subjects.
bdi_scores <- c(12, 18, 15, 22)
print(bdi_scores)
## [1] 12 18 15 22
# A character vector of participants IDs
subject_ids <- c("A", "B", "C", "D")
print(subject_ids)
## [1] "A" "B" "C" "D"
2. Factors: The Grouping Mechanism
A Factor is a special type of vector designed exclusively for categorical data.
Levels: Factors have “levels”, which are the predefined, valid categories. If you have a factor with
levels “Control” and “Treatment”, and you accidentally misspelled treatment as “Tretment”, R will
reject it, protecting your data integrity.
Under the Hood: R secretly stores factors as integer vectors (1, 2, 3…) but assigns text labels to
them. This is absolutely necessary for running grouping statistics like t-test and ANOVAs.
Example:
# First, create a standard character vector.
group_assignment <- c("Control", "Treatment", "Control", "Treatment")
# Convert it to strict factor for the statistical analysis with the help of the fo
llowing command
group_factor <- factor(group_assignment)
print(group_factor)
## [1] Control Treatment Control Treatment
## Levels: Control Treatment
3. Lists: The “Everything Bag”
Sometimes, you need to store data belonging to a single participants, but that data includes their name
(character), their trial scores (Numeric Vector), and whether they finished the task (Logical). You cannot put
these in a vector because they are different data types.
Heterogeneity: A List is a one-dimensional structure, but unlike. a vector, and even another list, all
inside one container.
Psychology Use Case: List are often used by R to output the result of complex statistical models.
When you run an ANOVA, R spits the results back to you as a massive list containing the p-values,
F-statistics, and degrees of freedom.
subje_profile <- list(
id = "A",
passed_vision_test = T,
rt <- c(450.5, 412.0, 398.2)
)
print(subje_profile)
## $id
## [1] "A"
##
## $passed_vision_test
## [1] TRUE
##
## [[3]]
## [1] 450.5 412.0 398.2
4. Data Frames: The Master Spreadsheet.
The data frame is the holy grail of R data structure. 99% of your psychology data analysis will happen
inside a data frame.
The Matrix Structure: a data frame is a two dimensional, rectangular table (rows and columns),
exactly like an Excel spreadsheet or an SPSS dataset.
The Architecture: Here is the genius of the data frame: Every column in a data frame is actually a
vector.
The Rules: Because every column is a vector, every column must be the same data type vertically.
However, different columns can be different types horizontally. Most importantly, every column must
be the exact same length. You cannot have 50 subject IDs and only 48 reaction times.
example
experimental_data <- [Link](
subject = subject_ids, #column 1: Charater vector
group = group_factor, # column 2: Factor vector
BDI_scores = bdi_scores # column 3: Numeric vecetor
)
print(experimental_data)
## subject group BDI_scores
## 1 A Control 12
## 2 B Treatment 18
## 3 C Control 15
## 4 D Treatment 22
Data Structure Dimensions Data Types Allowed Psychology Application
Vector 1D (Length) Homogeneous (Only A single column of
ONE type) survey scores.
List 1D (Length) Heterogeneous (Mixed Complex outputs (like an
types) ANOVA result object).
Matrix 2D (Rows x Cols) Homogeneous (Only Mathematical
ONE type) transformations, image
processing.
Data Frame 2D (Rows x Cols) Heterogeneous Your master participant
(Different types per spreadsheet.
column)
Your Exercise
You are setting up a small dataset for a short-term memory task (recalling a list of 20 words). You have
three participants.
Write the R code to complete the following:
1. Create a numeric vector named recall_scores containing the values: 14, 18, and 12.
recall_scores <- c(14, 18, 12)
print(recall_scores)
## [1] 14 18 12
2. Create a character vector named condition containing the values: “Silence”, “Noise”, “Silence”.
conditions<- factor(c('Silence', 'Noise', 'Silence'))
print(conditions)
## [1] Silence Noise Silence
## Levels: Noise Silence
3. Combine these two vectors into a Data Frame named memory_study . Name the first column
Score and the second column Environment .
memory_study <- [Link](
Score = recall_scores,
Environment = conditions
)
print(memory_study)
## Score Environment
## 1 14 Silence
## 2 18 Noise
## 3 12 Silence
These notes are prepared by YKB PSYCHOLOGY