In R, you can iterate over a Two-Dimensional List using nested for loops. The outer loop iterates over rows, and the inner loop iterates over columns. This allows you to access and process each element in the 2D list.
In this example,
matrix <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, byrow = TRUE)
for (row in 1:nrow(matrix)) {
for (col in 1:ncol(matrix)) {
print(matrix[row, col])
}
}
[1] 1 [1] 2 [1] 3 [1] 4 [1] 5 [1] 6
In this example,
matrix <- matrix(c('apple', 'banana', 'cherry', 'orange'), nrow = 2, byrow = TRUE)
for (row in 1:nrow(matrix)) {
for (col in 1:ncol(matrix)) {
print(matrix[row, col])
}
}
[1] 'apple' [1] 'banana' [1] 'cherry' [1] 'orange'
In this tutorial, we learned How to Iterate over a Two Dimensional List in R language with well detailed examples.