Mastering R’s syntax and core operators sets the foundation for advanced data analysis. In this post, you’ll learn how to assign variables, work with vectors, explore logical operations, and manage your R workspace effectively. By the end, you’ll understand the building blocks that power every script, function, and package you’ll use on your R journey.
1. Basic Arithmetic and Assignment
Every calculation in R relies on a handful of arithmetic operators and a clear way to store results.
Arithmetic operators
+addition-subtraction*multiplication/division^exponentiation
# Simple calculations
3 + 5 # 8
10 * 2 # 20
(4 - 1) / 3 # 1
2^3 # 8
Assignment operators
<-is the idiomatic arrow for assigning a value to a variable=works similarly in most contexts, though<-is preferred in scripts
# Variable assignment
x <- 42
y = x * 2
# Inspect values
x # 42
y # 84
Adopting a consistent assignment style makes your code easier to read and review. Save complex expressions to intermediate variables to improve clarity:
mean_value <- (3 + 5 + 7) / 3
2. Working with Vectors
Vectors are one-dimensional collections of elements of the same type. They underpin nearly every data structure in R.
Creating vectors
Use
c()to combine values into a vectorGenerate sequences with
:orseq()
numeric_vec <- c(1, 2, 3, 4, 5)
char_vec <- c("apple", "banana", "cherry")
seq_vec <- seq(from = 0, to = 1, by = 0.2)
Indexing and subsetting
Access elements by position (
[ ])Use logical vectors to filter values
numeric_vec[2] # 2
numeric_vec[c(1, 3, 5)] # 1 3 5
# Filter values greater than 3
numeric_vec[numeric_vec > 3] # 4 5
Vectorized operations
Apply arithmetic and functions element-wise
Recycling rules repeat shorter vectors
a <- c(10, 20, 30)
b <- c(1, 2)
a + b # 11 22 31 (2 recycled)
log(a) # natural log of each element
Mastering vectors lets you avoid explicit loops for many tasks, leading to cleaner and faster code.
3. Logical Operators
Logical operators allow you to compare values and control the flow of your analysis.
Comparison operators
>greater than<less than==equal to!=not equal to>=greater than or equal to<=less than or equal to
5 > 3 # TRUE
2 == 2 # TRUE
4 != 4 # FALSE
Logical connectors
&element-wise AND|element-wise OR&&short-circuit AND (first element only)||short-circuit OR (first element only)
# Element-wise logical
c(TRUE, FALSE, TRUE) & c(TRUE, TRUE, FALSE) # TRUE FALSE FALSE
# Short-circuit logical (useful in control flow)
if (length(numeric_vec) > 0 && mean(numeric_vec) > 3) {
print("Vector has values and mean exceeds 3")
}
Use logical operators to build filters, conditionals, and more robust data pipelines.
4. Workspace Management
A tidy workspace prevents confusion and ensures scripts run reliably each time.
Listing objects
ls()shows all variables and functions in memoryls(pattern = "df")filters by name
ls() # lists x, y, numeric_vec, char_vec, etc.
ls(pattern = "vec")# lists numeric_vec, char_vec, seq_vec
Removing objects
rm()deletes specified variablesrm(list = ls())clears the entire workspace
rm(y) # removes y only
rm(list = ls()) # start fresh
Saving and loading sessions
save.image("workspace.RData")saves everything in your workspaceload("workspace.RData")restores saved objects
# Save current state
save.image("my_analysis.RData")
# Later or in a new session
load("my_analysis.RData")
Regularly clearing and saving your workspace avoids unintended side effects when rerunning scripts and makes your analyses reproducible.
Conclusion and Next Steps
In this post, you’ve mastered R’s basic arithmetic, assignment operators, vector operations, logical comparisons, and workspace management tools. These fundamentals are the bedrock of every data manipulation and modeling task you’ll undertake in R.
Next up, we’ll dive into Data Structures—exploring matrices, lists, data frames, and tibbles, and learning how to choose the right structure for your data. If you have questions about assignments, vector quirks, or workspace tips, leave a comment below. Your feedback guides our journey, and I’ll address common pain points in upcoming posts. Happy coding!

Comments
Post a Comment