Matrix Operations in R Programming
Matrix Operations in R Programming
1. **Create a Matrix:**
```
```
2. **Access Elements:**
```
mat[row_index, col_index]
```
3. **Change Elements:**
```
```
```
```
```
```
6. **Matrix Dimensions:**
```
dim(matrix)
```
7. **Transpose a Matrix:**
```
t(matrix)
```
8. **Matrix Multiplication:**
```
```
9. **Element-wise Operations:**
```
sum(matrix)
mean(matrix)
min(matrix)
max(matrix)
```
```
rowSums(matrix)
colSums(matrix)
rowMeans(matrix)
colMeans(matrix)
```
```
matrix[, col_index]
matrix[row_index, ]
```
```
diag(matrix)
```
```
det(matrix)
solve(matrix)
```
```
eigen(matrix)
```
```
```
```
```
```
```
is.na(matrix)
```
```
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:
---
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`
8. **Logical Operations:**
```
vector1 > vector2
```
---
Factors
Certainly! Here's the list of various operations you can perform on factors in R, formatted for easy
copying into Word:
---
1. **Create a Factor:**
```
```
```
levels(factor)
```
```
4. **Factor Length:**
```
length(factor)
```
5. **Factor Count:**
```
table(factor)
```
6. **Factor Summary:**
```
summary(factor)
```
```
as.numeric(factor)
```
```
as.character(factor)
```
```
factor(vector, levels = ordered_levels, ordered = TRUE)
```
```
unique(factor)
```
```
is.na(factor)
```
```
sort(factor)
```
```
merge(factor1, factor2)
```
```
relevel(factor, new_order)
```
```
as.integer(factor)
```
```
table(factor)
```
```
```
```
```
```
```
```
as.logical(factor)
```
---
Arrays
---
**Array Operations in R:**
1. **Create an Array:**
```
```
2. **Access Elements:**
```
```
3. **Change Elements:**
```
```
4. **Array Dimensions:**
```
dim(array)
```
```
dimnames(array)
```
6. **Array Reshaping:**
```
```
7. **Transpose an Array:**
```
```
```
sum(array)
mean(array)
min(array)
max(array)
```
```
```
```
is.na(array)
```
```
```
```
```
```
```
```
```
```
drop(array)
```
```
replicate(times, array)
```
19. **Array Binding by Dimension:**
```
```
```
```
Dataframes
---
```
```
2. **Access Columns:**
```
data_frame$column_name
```
3. **Access Rows:**
```
data_frame[row_index, ]
```
4. **Change Column Values:**
```
```
```
dim(data_frame)
```
```
summary(data_frame)
```
```
```
8. **Remove Column:**
```
```
```
subset(data_frame, condition)
```
```
```
```
```
```
```
```
```
```
```
```
```
pivoted_data <- pivot_wider(data_frame, names_from = column, values_from = value_column)
```
```
```
```
is.na(data_frame)
```
```
```
```
```
```
colSums(data_frame)
colMeans(data_frame)
```
```
subset(data_frame, column_name %in% c("value1", "value2"))
```
Lists
---
1. **Create a List:**
```
```
2. **Access Elements:**
```
my_list[[index]]
```
```
my_list$element_name
```
```
```
5. **List Length:**
```
length(my_list)
```
6. **Add New Element:**
```
```
7. **Remove Element:**
```
```
8. **List Subsetting:**
```
```
```
```
```
```
```
unlist(my_list)
```
12. **List to Data Frame Conversion:**
```
```
```
```
```
```
```
```
```
```
```
```
lapply(my_list, function)
```
```
```
```
length(my_list) == 0
```
---