Learn R – Part 2

Vectors are 1D data structures holding elements of the same type .

Sort : sort() (ascending) or rev(sort()) (descending).

Create : Use c(), seq(from, to, by), or rep(value, times).

Access : Use [index] (positive/negative), logical vectors, or names.

Length : length(vector).

# HW1: Create a vector of even numbers 2, 4, 6 using `seq()`  
# HW2: Access the 3rd element of `c(10, 20, 30, 40)`  
# HW3: Sort `c(5, 1, 3)` in descending order  
# HW4: Check the length of `c("a", "b", "c")`  

# HW5: Create a vector with 3 copies of "R" using `rep()`  
R
# HW1  
vec_seq <- seq(2, 6, by=2)  # Output: 2, 4, 6  
# HW2  
print(c(10, 20, 30, 40)[3])  # Output: 30  
# HW3  
sorted <- rev(sort(c(5, 1, 3)))  # Output: 5, 3, 1  
# HW4  
print(length(c("a", "b", "c")))  # Output: 3  
# HW5  
vec_rep <- rep("R", 3)  # Output: "R", "R", "R"  

Lists store mixed or nested data .

Add/Remove : list[[new_index]] <- value or list[index] <- NULL.

Create : list().

Access : [index] (returns sublist), [[index]] (returns element), or $name.

Modify : Assign new values via [[ ]] or append().

# HW1: Create a list with "apple", 25, and a sub-list `c(1, 2)`  
# HW2: Access the sub-list `c(1, 2)` from HW1  
# HW3: Change "apple" to "banana" in the list  
# HW4: Add `TRUE` to the end of the list  

# HW5: Remove the 2nd element (25)  
R
# HW1  
my_list <- list("apple", 25, list(1, 2))  
# HW2  
print(my_list[[3]])  # Output: 1, 2  
# HW3  
my_list[[1]] <- "banana"  
# HW4  
my_list <- append(my_list, TRUE)  
# HW5  
my_list[2] <- NULL  

Matrices are 2D, same-type data structures.

Generate Values : seq(), rep().

Create : matrix(data, nrow, ncol).

Access : [row, col], [,] for entire rows/columns.

Add Rows/Columns : rbind(), cbind().

# HW1: Create a 3x2 matrix with 1-6 using `matrix()`  
# HW2: Extract the 2nd row  
# HW3: Extract the 1st column  
# HW4: Add a row `7, 8` to the matrix  

# HW5: Create a matrix with 1, 2 repeated 3 times using `rep()`  
R
# HW1  
mat <- matrix(1:6, nrow=3)  
# HW2  
print(mat[2, ])  # Output: 2, 5  
# HW3  
print(mat[, 1])  # Output: 1, 2, 3  
# HW4  
mat <- rbind(mat, c(7, 8))  
# HW5  
mat_rep <- matrix(rep(1:2, 3), nrow=3)  

Arrays extend matrices to multi-dimensional data .

Dimensions : dim() to check or set dimensions.

Create : array(data, dim=c(rows, cols, ...)).

Access : [i, j, k] for specific elements.

# HW1: Create a 2x2x2 array with values 1-8  
# HW2: Access the 3rd element of the 1st layer  
# HW3: Extract the 2nd layer (all rows/columns)  
# HW4: Check the total length of the array  

# HW5: Convert a vector `1:12` into a 3x4 array  
R
# HW1  
arr <- array(1:8, dim=c(2, 2, 2))  
# HW2  
print(arr[3])  # Output: 3  
# HW3  
print(arr[, , 2])  
# HW4  
print(length(arr))  # Output: 8  
# HW5  
arr_3d <- array(1:12, dim=c(3, 4))  

Data frames store tabular data (mixed types allowed).

Modify : Add/remove columns via $ or [ ].

Create : data.frame().

Access : $column, [, "column"], or subset().

# HW1: Create a data frame with Name (Alice, Bob), Age (25, 30)  
# HW2: Access the "Name" column using `$`  
# HW3: Add a column "Salary" with 5000, 6000  
# HW4: Remove the "Age" column  

# HW5: Check the number of rows  
R
# HW1  
df <- data.frame(Name=c("Alice", "Bob"), Age=c(25, 30))  
# HW2  
print(df$Name)  # Output: Alice, Bob  
# HW3  
df$Salary <- c(5000, 6000)  
# HW4  
df$Age <- NULL  
# HW5  
print(nrow(df))  # Output: 2  

Factors store categorical data with predefined levels.

Ordered Factors : ordered=TRUE for ranking.

Create : factor().

Modify Levels : levels(), factor(..., levels=).

# HW1: Create a factor with "Low", "Medium", "High"  
# HW2: Check the levels of the factor  
# HW3: Add "Very High" as a new level  
# HW4: Remove "Medium" from the factor  

# HW5: Convert the factor to an ordered factor  
R
# HW1  
f <- factor(c("Low", "Medium", "High"))  
# HW2  
print(levels(f))  # Output: "High", "Low", "Medium"  
# HW3  
f <- factor(f, levels=c("Low", "Medium", "High", "Very High"))  
# HW4  
f <- f[f != "Medium"]  
# HW5  
f_ordered <- factor(f, ordered=TRUE)  

Leave a Reply

Your email address will not be published. Required fields are marked *