Here’s a detailed breakdown of Unit III topics,
with bullet points for each:
Vectors
Creating and Naming Vectors
Use the c() function to combine items into a
vector.
Elements in a vector must share the same
data type (e.g., numeric, character, logical).
Naming vector elements can be achieved
using names(vector) <- c("name1",
"name2").
Create sequences with : (colon operator) or
seq() for numeric vectors.
The assign() function can assign vectors
dynamically by name.
Use typeof() to check the type of a vector.
Vectors are indexed starting at 1 in R, unlike
other languages.
Vector Arithmetic
Supports element-wise operations: addition,
subtraction, multiplication, and division.
Vectors must be of the same length;
otherwise, shorter vectors are recycled.
Examples include v1 + v2 (element-wise
addition).
Operands can also include constants (e.g., v1
* 2).
Logical operations return Boolean vectors
based on conditions.
Vector Subsetting
Access individual elements using [index]
(e.g., v[1] for the first element).
Use logical vectors to extract elements that
satisfy a condition (e.g., v[v > 10]).
Character indexing accesses named
elements in a vector (e.g., v["name"]).
Use ranges (v[1:3]) to subset multiple
elements.
Negative indices exclude elements (e.g.,
v[-1] excludes the first element).
Matrices
Creating and Naming Matrices
Create matrices with the matrix() function by
specifying data, nrow, ncol.
Default matrix creation fills elements
column-wise; set byrow = TRUE to fill row-
wise.
Names can be assigned to rows and columns
using the dimnames parameter.
Example:
matrix(data, nrow = 2, ncol = 3, byrow = TRUE,
dimnames = list(c("R1", "R2"), c("C1", "C2",
"C3")))
Matrix Subsetting
Access individual elements using [row,
column].
Extract entire rows with [row, ] and columns
with [, column].
Use ranges to extract subsets (e.g.,
matrix[1:2, 2:3]).
Modify elements by directly assigning values
(e.g., matrix[1, 2] <- 10).
Arrays
Creating Arrays
Arrays are created with the array() function,
specifying dimensions and data.
Dimensions are passed using the dim
argument, e.g., (2, 3, 4) for 2 rows, 3 columns,
and 4 matrices.
Names for rows, columns, and matrices can
be assigned using dimnames.
Accessing and Manipulating Arrays
Use multi-dimensional indexing to access
elements, rows, or columns.
Example: array[1, 2, 3] accesses the element
in row 1, column 2, matrix 3.
Perform arithmetic operations element-wise,
e.g., array1 + array2.
Use apply() for calculations across
dimensions.
Factors
Introduction to Factors
Factors represent categorical data in R,
created using factor().
Factor levels define the unique categories,
e.g., "Male", "Female".
Summarizing a factor shows the count of
each level.
Ordered Factors
Ordered factors are created with ordered =
TRUE.
Allows comparison using relational operators
(<, >).
Example:
factor(c("Low", "Medium", "High"), ordered =
TRUE)
Comparing Ordered Factors
Use comparison operators to evaluate
precedence, e.g., "High" > "Low".
Data Frames
Introduction to Data Frames
Data frames are tabular structures where
columns can have different types.
Created using the data.frame() function.
Each column can represent a vector of data.
Subsetting Data Frames
Access elements using [row, column].
Use $ to access columns by name, e.g.,
df$column.
Logical indexing subsets rows based on
conditions, e.g., df[df$Age > 30, ].
Extending and Sorting Data Frames
Add columns with df$new_col <- values.
Add rows with rbind(df, new_row).
Sort data frames with order(), e.g.,
df[order(df$column), ].
Lists
Introduction to Lists
Lists can hold objects of different types, such
as vectors, matrices, and data frames.
Created using list() function.
Creating a Named List
Assign names during creation, e.g.,
list(name1 = vector1, name2 = matrix1).
Accessing and Manipulating List Elements
Access elements using $ or double square
brackets [[ ]].
Modify elements by assigning new values,
e.g., list$name <- new_value.
Merging Lists and Converting to Vectors
Combine lists using c(list1, list2).
Convert to vectors using unlist(), provided all
elements are compatible.
Let me know if you’d like further details or
additional explanations!