arrNumber <- matrix(c(6, 8, 9, 16, 18, 19, 27, 36, 45, 54, 63, 72), nrow = 3, ncol = 4)
arrNumber
## [,1] [,2] [,3] [,4]
## [1,] 6 16 27 54
## [2,] 8 18 36 63
## [3,] 9 19 45 72
arrFoods <- matrix(c('Fish', 'Meat', 'Chicken', 'Cheese', 'Milk', 'Butter', 'Eggs', 'Vegetables', 'Fruit', 'Rice', 'Bread', 'Steak'), nrow = 3, ncol = 4)
arrFoods
## [,1] [,2] [,3] [,4]
## [1,] "Fish" "Cheese" "Eggs" "Rice"
## [2,] "Meat" "Milk" "Vegetables" "Bread"
## [3,] "Chicken" "Butter" "Fruit" "Steak"
#Number of rows and columns
dim(arrFoods)
## [1] 3 4
#Matrix length
length(arrFoods)
## [1] 12
#Check if an item exists
if("Chicken" %in% arrFoods){
print("TRUE")
}else{
print("FALSE")
}
## [1] "TRUE"
#Access matrix items
arrFoods[1,2]
## [1] "Cheese"
arrFoods[, 3]
## [1] "Eggs" "Vegetables" "Fruit"
arrFoods[3, ]
## [1] "Chicken" "Butter" "Fruit" "Steak"
arrFoods[, -3]
## [,1] [,2] [,3]
## [1,] "Fish" "Cheese" "Rice"
## [2,] "Meat" "Milk" "Bread"
## [3,] "Chicken" "Butter" "Steak"
#Add rows and columns
c_arrFoods <- cbind(arrFoods, c('Lemons', 'Plums', 'Papayas'))
c_arrFoods
## [,1] [,2] [,3] [,4] [,5]
## [1,] "Fish" "Cheese" "Eggs" "Rice" "Lemons"
## [2,] "Meat" "Milk" "Vegetables" "Bread" "Plums"
## [3,] "Chicken" "Butter" "Fruit" "Steak" "Papayas"
arrFoods <- matrix(c('Fish', 'Meat', 'Chicken', 'Cheese', 'Milk', 'Butter', 'Eggs', 'Vegetables', 'Fruit', 'Rice', 'Bread', 'Steak'), nrow = 3, ncol = 4)
r_arrFoods <- rbind(arrFoods, c('Coconuts', 'Mangoes', 'Pears','Bananas'))
r_arrFoods
## [,1] [,2] [,3] [,4]
## [1,] "Fish" "Cheese" "Eggs" "Rice"
## [2,] "Meat" "Milk" "Vegetables" "Bread"
## [3,] "Chicken" "Butter" "Fruit" "Steak"
## [4,] "Coconuts" "Mangoes" "Pears" "Bananas"
#remove rows and columns
arrFoods <- matrix(c('Fish', 'Meat', 'Chicken', 'Cheese', 'Milk', 'Butter', 'Eggs', 'Vegetables', 'Fruit', 'Rice', 'Bread', 'Steak'), nrow = 3, ncol = 4)
arrFoods
## [,1] [,2] [,3] [,4]
## [1,] "Fish" "Cheese" "Eggs" "Rice"
## [2,] "Meat" "Milk" "Vegetables" "Bread"
## [3,] "Chicken" "Butter" "Fruit" "Steak"
remove_arrFoods <- arrFoods[-c(1), -c(1)]
remove_arrFoods
## [,1] [,2] [,3]
## [1,] "Milk" "Vegetables" "Bread"
## [2,] "Butter" "Fruit" "Steak"
#Loop through a Matrix
i <- 0
for(rows in 1:nrow(arrFoods)){
for(columns in 1:ncol(arrFoods)){
i = i + 1
cat(i, ". ", arrFoods[rows, columns], "\n", sep = "")
}
}
## 1. Fish
## 2. Cheese
## 3. Eggs
## 4. Rice
## 5. Meat
## 6. Milk
## 7. Vegetables
## 8. Bread
## 9. Chicken
## 10. Butter
## 11. Fruit
## 12. Steak
arrFoods
## [,1] [,2] [,3] [,4]
## [1,] "Fish" "Cheese" "Eggs" "Rice"
## [2,] "Meat" "Milk" "Vegetables" "Bread"
## [3,] "Chicken" "Butter" "Fruit" "Steak"
arrNumber
## [,1] [,2] [,3] [,4]
## [1,] 6 16 27 54
## [2,] 8 18 36 63
## [3,] 9 19 45 72
#Adding it as a rows
arr_combined_rows <- rbind(arrFoods, arrNumber)
arr_combined_rows
## [,1] [,2] [,3] [,4]
## [1,] "Fish" "Cheese" "Eggs" "Rice"
## [2,] "Meat" "Milk" "Vegetables" "Bread"
## [3,] "Chicken" "Butter" "Fruit" "Steak"
## [4,] "6" "16" "27" "54"
## [5,] "8" "18" "36" "63"
## [6,] "9" "19" "45" "72"
#Adding it as a columns
arr_combined_columns <- cbind(arrFoods, arrNumber)
arr_combined_columns
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
## [1,] "Fish" "Cheese" "Eggs" "Rice" "6" "16" "27" "54"
## [2,] "Meat" "Milk" "Vegetables" "Bread" "8" "18" "36" "63"
## [3,] "Chicken" "Butter" "Fruit" "Steak" "9" "19" "45" "72"