A scalar is a single numeric, character, or logical value in R.
> scalar <- 5
A vector is a sequence of data elements of the same type (numeric, character, or logical). It is created using a c().
> vector <- c(1, 2, 3, 4)
A vector can act as a standalone variable. It can also be a column in a data frame, where it represents one dimension of data.
> df <- data.frame(ID = c(1, 2, 3), Age = c(20, 25, 30))
> print(df)
We can perform arithmatic & logical operations. Basic operations such as addition, substraction, multiplication, division:
> 5 + 3 # 8
> 5 – 3 # 2
> 5 * 3 # 15
> 5 / 3 # 1.666…
Exponential & Square Root:
> 5^3 # 125
> sqrt(25) # 5
Logarithmic and exponential functions:
> log(10) # Natural log
> exp(2) # Exponential of 2
Mathematical Constants:
> pi # 3.14159265358979
Absolute & Rounding Functions:
> abs(-5) # 5 absolute
> round(3.14159, 2) # 3.14
> signif(3.14159, 3) # 3.14 (3 significant digits)
Modulo and integer division:
> 7 %% 3 # 1 (remainder)
> 7 %/% 3 # 2 (integer quotient)
Logical operations:
> 5 == 3 # FALSE
> 5 != 3 # TRUE
> 5 > 3 # TRUE
> 5 >= 3 # TRUE
Check membership and pipe operator:
> 3 %in% c(1, 2, 3) # TRUE
> c(1, 2, 3) %>% mean() # 2 (requires dplyr)
Sequence & Repitition:
> seq(2, 8, by = 2) # 2, 4, 6, 8
> seq(1, 5, length.out = 3) # 1, 3, 5
> rep(1:3, times = 2) # 1, 2, 3, 1, 2, 3
> rep(1:3, each = 2) # 1, 1, 2, 2, 3, 3
Factorial and combination:
> factorial(5) # 120
> choose(5, 3) # 10 (combinations)
Random sampling:
> sample(1:10, 5) # Random 5 numbers from 1 to 10
Extract unique elements:
> unique(c(1, 2, 2, 3)) # 1, 2, 3
Cumulative operations:
> cumsum(1:4) # 1, 3, 6, 10
> cumprod(1:4) # 1, 2, 6, 24
Sorting and ranking:
> sort(c(3, 1, 2)) # 1, 2, 3
> order(c(3, 1, 2)) # 2, 3, 1 (indices)
> rank(c(3, 1, 2)) # 3, 1, 2
Create and operate on matrices:
> mat <- matrix(1:9, nrow = 3)
> t(mat) # Transpose
> mat %*% mat # Matrix multiplication
> diag(mat) # Extract diagonal
Adding scalar & vector as objects:
> x <- 10
> y <- 20
> sum <- x + y # 30
> vec <- c(1, 2, 3, 4, 5)
> sum_vec <- sum(vec) # Sum of all elements: 15
> df <- data.frame(Name = c(“A”, “B”, “C”), Age = c(25, 30, 35)) # vector in a data frame
> print(df)
Verifying Type and Class (structure):
Type of a scalar
> x <- 5
> typeof(x) # “double”
Type of a vector
> vec <- c(1, 2, 3)
> class(vec) # “numeric”
When creating vectors with mixed types, R coerces them to a common type:
> mix_vec <- c(1, “a”, TRUE)
> print(mix_vec) # “1” “a” “TRUE” (all converted to characters)
Accessing (indexing & subsetting) elements in vectors:
> vec <- c(10, 20, 30, 40)
> vec[2] # 20 (accessing single element)
> vec[c(1, 3)] # 10, 30 (accessing multiple elements)
Logical indexing:
> vec[vec > 25] # 30, 40 (elements greater than 25)
Assign names (Named Vectors) to vector elements for easier access:
> scores <- c(Alice = 90, Bob = 85, Carol = 88)
> scores[“Alice”] # 90
Vectorized operations allow element-wise calculations:
> vec1 <- c(1, 2, 3)
> vec2 <- c(4, 5, 6)
> vec1 + vec2 # 5, 7, 9
> vec1 * vec2 # 4, 10, 18
Recycling:
> vec1 <- c(1, 2, 3)
> vec2 <- c(10, 20)
> vec1 + vec2 # 11, 22, 13 (recycles vec2)
Using Functions on Vectors: Aggregation
> max(vec) # Maximum value
> min(vec) # Minimum value
> mean(vec) # Mean
> median(vec) # Median
> var(vec) # Variance
> sd(vec) # Standard deviation
Applying functions to each element:
> sqrt(vec) # Square root of each element
> log(vec) # Logarithm of each element
match()
to find the position of a value:
> vec <- c(“apple”, “banana”, “cherry”)
> match(“banana”, vec) # 2
which()
to find the indices meeting a condition:
> vec <- c(10, 20, 30, 40)
> which(vec > 25) # 3, 4
Bind (combining) vectors to create matrices or data frames:
> row1 <- c(1, 2, 3)
> row2 <- c(4, 5, 6)
> mat <- rbind(row1, row2) # Matrix with rows
> print(mat)
> df <- data.frame(Name = c(“A”, “B”), Score = c(90, 85))
> print(df)
Handling Missing Data – Introduce NA
(missing values) and how to handle them:
> vec <- c(1, 2, NA, 4)
> is.na(vec) # FALSE, FALSE, TRUE, FALSE (checking for missing values)
> vec <- na.omit(vec) # removing missing values
> vec[is.na(vec)] <- 0 # replacing missing values
Vectorized Logical Conditions:
> vec <- c(10, 20, 30)
> all(vec > 5) # TRUE
> any(vec > 25) # TRUE
Using apply()
with Vectors:
> vec <- c(1, 2, 3)
> sapply(vec, function(x) x^2) # Square each element
Vector memory management with length() and object.size()
> vec <- 1:100000
> object.size(vec) # Size of the object in memory