Introduction to R for Psychology.
To learn R language for psychological statistical analysis, first we need to understand the environment the
Integrated Development Environment (IDE) of RStudio.
The Ecosystem: What is R and RStudio.
R is the underlying programming language. It is like a powerful, invisible calculator designed specifically for
statistical analysis and data visualization.
To make the workflow efficient and organised, we use RStudio which is an IDE. It is the dashboard that
sits on the top of the R engine.
The R Engine: Handles the math, allocates computer memory, and runs the statistical algorithms.
The RStudio IDE: Provides the visual framework, file management, plotting screens, and code
highlighting.
The Relationship: You write your commands in RStudio, which then secretly feeds them to R, gets
the answers, and displays them beautifully back to you.
Ecosystem Component What it is Role in Research
R The computational engine Processes datasets and runs
statistical tests in the
background.
RStudio The visual interface The dashboard where you write
code, manage files, and view
plots.
The Environment.
RStudio divides the digital work-space into four distinct quadrant, each dedicated to a specific phase of
the data analysis lifecycle.
1. The Source (Top-Left): This is a text editor where you write reproducible .R scripts. Code typed
here is static; it does not execute until you highlight it and command it to run.
2. The Console (Bottom-left): The execution floor. This is your direct line to the R-engine. When you
run code from the Source, it drop down here to be evaluated. It has no permanent memory.
3. The Environment (Top-Right): Your active working memory (RAM). When you load a dataset, R
pulls it into active memory so it can b manipulated. Every variable you create appears here.
4. The Output (Bottom-Right): The perceptual display. This multi-tabbed window shows your file
directories, installed packages, help documentation, and rendered plots.
Pane Location Tab Name Primary Function for
Psychology Researchers
Top-Left Source Writing permanent, reproducible
analysis scripts.
Bottom-Left Console The live engine where code is
evaluated and output is printed.
Top-Right Environment A visual list of all data and
variables currently loaded in
active memory.
Bottom-Right Output Rendering visualizations, viewing
files, and reading documentation.
Object Assignment: Injecting into Memory.
In R, data must be stored in an Object (a named container in your computer’s memory) so it can be
referenced later.
To put data into an object, R uses the Assignment Operator: <-
Directional Logic: It explicitly tells R to evaluate the raw value on the right, and inject it into the
container named on the left.
Overwriting: If you assign a new value to an object that already exists, R will overwrite the old data
instantly without a warning prompt.
Best Practice: While the equals sign ( = ) works, <- is the rigorous academic standard because it
clarifies the exact direction of data flow.
Example Code:
anxiety_score <- 45
print(anxiety_score)
## [1] 45
Atomic Data Types: The Fundamental Building Blocks
R is a strictly typed language. It needs to know exactly what kind of information it is holding because you
cannot run mathematical models on text. Everything you import into R is broken down into fundamental
data types.
Numeric: The default for any continuous or discrete number (e.g., reaction times, decimals).
Integer: Strict whole numbers. You must attach a capital L to force R to save it as an integer, which
saves computer memory (e.g., trial counts).
Character: Text data. You must wrap character data in quotation marks, otherwise R will think you
are typing the name of an object and throw an error.
Logical: A binary switch ( TRUE or FALSE ). Must be typed in all caps without quotes. Used heavily
for filtering datasets.
Raw: Hexadecimal bytes (the raw 1s and 0s of computer data). Note: both complex and raw data in
highly rare in psychological research
Factors: How R handles categorical independent variables (e.g., “Control” vs. “Treatment”). R
secretly stores them as numbers but attaches character labels to them for grouping in ANOVAs.
Data Type R Definition Psychology Use Case Code Syntax Example
Numeric Numbers (decimals or Reaction time (ms) rt <- 345.8
whole)
Integer Strict whole numbers Count of correct recalls correct_trials <- 15L
Character Text strings (requires Subject diagnosis group <- "Bipolar_1"
quotes) category
Raw Hexadecimal bytes Rarely used in my_raw <-
psychology charToRaw(“Hello”)
Logical Binary TRUE/FALSE Passed attention is_valid <- TRUE
(ALL CAPS) check?
my_number <- 10L # The 'L' suffix indicates that this is an integer literal
print(my_number) # This will print the value of my_number, which is 10
## [1] 10
typeof(my_number) # This will print the type of my_number, which is "integer"
## [1] "integer"
my_double <- 10.5 # This is a numeric literal, which is treated as a double by def
ault
print(my_double) # This will print the value of my_double, which is 10.
## [1] 10.5
typeof(my_double) # This will print the type of my_double, which is "double"
## [1] "double"
my_string <- "Hello, R!" # This is a character string literal
print(my_string) # This will print the value of my_string, which is "Hello, R!"
## [1] "Hello, R!"
typeof(my_string) # This will print the type of my_string, which is "character"
## [1] "character"
my_logical <- TRUE # This is a logical literal
print(my_logical) # This will print the value of my_logical, which is TRUE
## [1] TRUE
typeof(my_logical) # This will print the type of my_logical, which is "logical"
## [1] "logical"
my_complex <- 1 + 2i # This is a complex number literal
print(my_complex) # This will print the value of my_complex, which is 1 + 2i
## [1] 1+2i
typeof(my_complex) # This will print the type of my_complex, which is "complex"
## [1] "complex"
my_raw <- charToRaw("Hello") # This creates a raw vector from the string "Hello"
print(my_raw) # This will print the raw vector representation of "Hello"
## [1] 48 65 6c 6c 6f
typeof(my_raw) # This will print the type of my_raw, which is "raw"
## [1] "raw"
my_factor <- factor(c('low', 'medium', 'high')) # This creates a factor from a cha
racter vector
print(my_factor) # This will print the factor levels and their corresponding value
s
## [1] low medium high
## Levels: high low medium
typeof(my_factor) # This will print the type of my_factor, which is "integer"
## [1] "integer"
Your Exercise
You are setting up the initial variables for a single subject in a cognitive task. Provide the exact R code
block to create the following four objects, paying strict attention to the required data types:
1. Create an object called subject_id and assign it the character text: Subj_001
subject_id <- "subj_001"
print(subject_id)
## [1] "subj_001"
2. Create an object called age and assign it the strict integer value of 25.
age <- 25L
print(age)
## [1] 25
3. Create an object called baseline_rt and assign it the numeric value of 450.75
baseline_rt <- 450.75
4. Create an object called is_medicated and assign it the logical value of false.
is_medicated <- FALSE
print(is_medicated)
## [1] FALSE
These notes are prepared by YKB PSYCHOLOGY