0% found this document useful (0 votes)
47 views

Matrix Operations in R Programming

The document provides an overview of various matrix operations in R, including how to create, access, change, and perform other common operations on matrices. Some key points covered include how to create and dimension matrices, access and change matrix elements, combine matrices, perform element-wise and other calculations on matrices, and extract subsets of matrices.

Uploaded by

Ruthvik Racha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Matrix Operations in R Programming

The document provides an overview of various matrix operations in R, including how to create, access, change, and perform other common operations on matrices. Some key points covered include how to create and dimension matrices, access and change matrix elements, combine matrices, perform element-wise and other calculations on matrices, and extract subsets of matrices.

Uploaded by

Ruthvik Racha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Matrix Operations in R:

1. **Create a Matrix:**

```

matrix(data, nrow, ncol, byrow = FALSE)

```

2. **Access Elements:**

```

mat[row_index, col_index]

```

3. **Change Elements:**

```

mat[row_index, col_index] <- new_value

```

4. **Combine Matrices by Rows (rbind):**

```

rbind(matrix1, matrix2, ...)

```

5. **Combine Matrices by Columns (cbind):**

```

cbind(matrix1, matrix2, ...)

```

6. **Matrix Dimensions:**

```

dim(matrix)
```

7. **Transpose a Matrix:**

```

t(matrix)

```

8. **Matrix Multiplication:**

```

mat1 %*% mat2

```

9. **Element-wise Operations:**

- Element-wise Addition: `mat1 + mat2`

- Element-wise Subtraction: `mat1 - mat2`

- Element-wise Multiplication: `mat1 * mat2`

- Element-wise Division: `mat1 / mat2`

- Element-wise Exponentiation: `mat ^ 2`

10. **Matrix Element Sum, Mean, etc.:**

```

sum(matrix)

mean(matrix)

min(matrix)

max(matrix)

```

11. **Matrix Row or Column Sum, Mean, etc.:**

```

rowSums(matrix)

colSums(matrix)
rowMeans(matrix)

colMeans(matrix)

```

12. **Matrix Subsetting and Indexing:**

13. **Row or Column Extraction:**

```

matrix[, col_index]

matrix[row_index, ]

```

14. **Extract Diagonal Elements:**

```

diag(matrix)

```

15. **Determinant and Inverse:**

```

det(matrix)

solve(matrix)

```

16. **Eigenvalues and Eigenvectors:**

```

eigen(matrix)

```

17. **Row or Column Bind by Name (cbind):**

```

cbind(matrix1, matrix2, ...)


```

18. **Row or Column Bind by Index (cbind):**

```

cbind(matrix1, matrix2, ...)

```

19. **Element-wise Logical Operations:**

```

mat1 > mat2

```

20. **Check for Missing Values:**

```

is.na(matrix)

```

21. **Matrix Reshaping:**

- Convert Matrix to Vector: `as.vector(matrix)`

- Convert Vector to Matrix: `matrix(vector, nrow, ncol)`

22. **Row or Column Names:**

```

rownames(matrix)

colnames(matrix)

```

Vectors
Certainly! Here's the list of various operations you can perform on vectors in R, formatted for easy
copying into Word:
---

**Vector Operations in R:**

1. **Create a Vector:**
```
c(element1, element2, ...)
```

2. **Access Elements:**
```
vector[index]
```

3. **Change Elements:**
```
vector[index] <- new_value
```

4. **Vector Length:**
```
length(vector)
```

5. **Combine Vectors:**
```
c(vector1, vector2, ...)
```

6. **Element-wise Operations:**
- Element-wise Addition: `vector1 + vector2`
- Element-wise Subtraction: `vector1 - vector2`
- Element-wise Multiplication: `vector1 * vector2`
- Element-wise Division: `vector1 / vector2`
- Element-wise Exponentiation: `vector ^ 2`

7. **Vector Sum, Mean, etc.:**


```
sum(vector)
mean(vector)
min(vector)
max(vector)
```

8. **Logical Operations:**
```
vector1 > vector2
```

9. **Check for Missing Values:**


```
is.na(vector)
```

10. **Sort a Vector:**


```
sort(vector)
```

11. **Find Unique Elements:**


```
unique(vector)
```
12. **Vector Subsetting and Indexing:**

13. **Extract Elements by Condition:**


```
vector[condition]
```

14. **Vector Element Recycling:**


```
rep(vector, times)
```

15. **Vector Element Repetition:**


```
rep(vector, each)
```

16. **Find Indices with Condition:**


```
which(condition)
```

17. **Vector Reshaping:**


- Convert Vector to Matrix: `matrix(vector, nrow, ncol)`
- Convert Matrix to Vector: `as.vector(matrix)`

18. **Append Elements to Vector:**


```
append(vector, new_elements)
```

19. **Vector Element Removal:**


```
vector[-index]
```

20. **Vector Names:**


```
names(vector)
```

---

Factors
Certainly! Here's the list of various operations you can perform on factors in R, formatted for easy
copying into Word:

---

**Factor Operations in R:**

1. **Create a Factor:**

```

factor(vector, levels = NULL, labels = levels)

```

2. **Access Levels of a Factor:**

```

levels(factor)

```

3. **Change Levels and Labels:**

```

levels(factor) <- new_levels


```

4. **Factor Length:**

```

length(factor)

```

5. **Factor Count:**

```

table(factor)

```

6. **Factor Summary:**

```

summary(factor)

```

7. **Factor Subset and Indexing:**

8. **Convert Factor to Numeric:**

```

as.numeric(factor)

```

9. **Convert Factor to Character:**

```

as.character(factor)

```

10. **Factor with Ordered Levels:**

```
factor(vector, levels = ordered_levels, ordered = TRUE)

```

11. **Find Unique Levels:**

```

unique(factor)

```

12. **Check for Missing Values:**

```

is.na(factor)

```

13. **Factor Sorting:**

```

sort(factor)

```

14. **Factor Merging:**

```

merge(factor1, factor2)

```

15. **Factor Reordering:**

```

relevel(factor, new_order)

```

16. **Factor to Numeric Encoding:**

```

as.integer(factor)
```

17. **Factor Frequency:**

```

table(factor)

```

18. **Subset Factor by Level:**

```

subset_factor <- factor[factor == "level"]

```

19. **Factor Renaming:**

```

levels(factor) <- new_labels

```

20. **Factor Renaming with Labels:**

```

factor <- factor(factor, labels = new_labels)

```

21. **Factor Conversion to Logical:**

```

as.logical(factor)

```

---

Arrays
---
**Array Operations in R:**

1. **Create an Array:**

```

array(data, dim = c(dim1, dim2, ...))

```

2. **Access Elements:**

```

array[index1, index2, ...]

```

3. **Change Elements:**

```

array[index1, index2, ...] <- new_value

```

4. **Array Dimensions:**

```

dim(array)

```

5. **Access Dimension Names:**

```

dimnames(array)

```

6. **Array Reshaping:**

```

dim(array) <- c(new_dim1, new_dim2, ...)

```
7. **Transpose an Array:**

```

aperm(array, perm = c(dim_order))

```

8. **Array Element-wise Operations:**

- Element-wise Addition: `array1 + array2`

- Element-wise Subtraction: `array1 - array2`

- Element-wise Multiplication: `array1 * array2`

- Element-wise Division: `array1 / array2`

9. **Array Sum, Mean, etc.:**

```

sum(array)

mean(array)

min(array)

max(array)

```

10. **Logical Operations on Arrays:**

```

array1 > array2

```

11. **Check for Missing Values:**

```

is.na(array)

```

12. **Array Subset and Indexing:**


13. **Array Reshaping to Matrix:**

```

matrix(array, nrow, ncol)

```

14. **Array Merging:**

```

abind(array1, array2, along = dimension)

```

15. **Array Apply Functions:**

```

apply(array, margin, FUN)

```

16. **Array Slice Extraction:**

```

array[start:end, start:end, ...]

```

17. **Array Dimension Collapse:**

```

drop(array)

```

18. **Array Replication:**

```

replicate(times, array)

```
19. **Array Binding by Dimension:**

```

abind(array1, array2, along = dimension)

```

20. **Array Names:**

```

dimnames(array) <- list(row_names, col_names, ...)

```

Dataframes

---

**Data Frame Operations in R:**

1. **Create a Data Frame:**

```

data.frame(column1, column2, ...)

```

2. **Access Columns:**

```

data_frame$column_name

```

3. **Access Rows:**

```

data_frame[row_index, ]

```
4. **Change Column Values:**

```

data_frame$column_name <- new_values

```

5. **Data Frame Dimensions:**

```

dim(data_frame)

```

6. **Data Frame Summary:**

```

summary(data_frame)

```

7. **Add New Column:**

```

data_frame$new_column <- values

```

8. **Remove Column:**

```

data_frame$column_name <- NULL

```

9. **Subset Data Frame:**

```

subset(data_frame, condition)

```

10. **Data Frame Filtering:**


```

data_frame[data_frame$column > threshold, ]

```

11. **Data Frame Sorting:**

```

sorted_data_frame <- data_frame[order(data_frame$column), ]

```

12. **Data Frame Merging:**

```

merged_data_frame <- merge(data_frame1, data_frame2, by = "common_column")

```

13. **Data Frame Grouping:**

```

grouped_data <- group_by(data_frame, group_column)

```

14. **Data Frame Aggregation:**

```

summarise(grouped_data, agg_column = aggregation_function(column))

```

15. **Data Frame Joining:**

```

joined_data <- inner_join(data_frame1, data_frame2, by = "common_column")

```

16. **Data Frame Pivot:**

```
pivoted_data <- pivot_wider(data_frame, names_from = column, values_from = value_column)

```

17. **Data Frame Melting:**

```

melted_data <- melt(data_frame, id.vars = c("id_columns"))

```

18. **Check for Missing Values:**

```

is.na(data_frame)

```

19. **Data Frame Reshaping:**

```

reshape(data_frame, direction = "long" or "wide", ...)

```

20. **Data Frame Rename Columns:**

```

names(data_frame) <- c("new_column_names")

```

21. **Data Frame Column Sum, Mean, etc.:**

```

colSums(data_frame)

colMeans(data_frame)

```

22. **Subset Data Frame by Column Values:**

```
subset(data_frame, column_name %in% c("value1", "value2"))

```

Lists
---

**List Operations in R:**

1. **Create a List:**

```

list(item1, item2, ...)

```

2. **Access Elements:**

```

my_list[[index]]

```

3. **Access Named Elements:**

```

my_list$element_name

```

4. **Change List Elements:**

```

my_list[[index]] <- new_value

```

5. **List Length:**

```

length(my_list)

```
6. **Add New Element:**

```

my_list$new_element <- value

```

7. **Remove Element:**

```

my_list[[element_name]] <- NULL

```

8. **List Subsetting:**

```

sub_list <- my_list[c(index1, index2, ...)]

```

9. **Named List Elements:**

```

names(my_list) <- c("name1", "name2", ...)

```

10. **Nested Lists:**

```

nested_list <- list(list1 = sub_list1, list2 = sub_list2)

```

11. **List Unlisting:**

```

unlist(my_list)

```
12. **List to Data Frame Conversion:**

```

data_frame <- as.data.frame(my_list)

```

13. **List Merge:**

```

merged_list <- c(my_list1, my_list2)

```

14. **List Element Check:**

```

exists("element_name", where = my_list)

```

15. **List to Matrix Conversion:**

```

matrix <- do.call(rbind, my_list)

```

16. **List Reverse:**

```

reversed_list <- rev(my_list)

```

17. **List Sorting:**

```

sorted_list <- sort(my_list)

```

18. **List Apply Functions:**


```

lapply(my_list, function)

```

19. **List Slicing:**

```

sliced_list <- my_list[start:end]

```

20. **Check if List is Empty:**

```

length(my_list) == 0

```

---

You might also like