← Back to R Course | Chapter 7: Data Structures | Lesson 5 of 7

Arrays

In this page:

  1. Arrays

Arrays

An R array generalizes a matrix to more than two dimensions, storing data in a multi-dimensional grid with array(data, dim = c(...)). Elements are accessed by specifying an index for each dimension, like a[1, 2, 3]. Arrays are less commonly used than matrices or data frames but are useful for genuinely multi-dimensional data.

Note: A 2-dimensional array is functionally the same as a matrix -- matrix() is just a convenient special case.

Example: Arrays

markup
arr <- array(1:24, dim = c(2, 3, 4))

cat("Dimensions:", dim(arr), "\n")
cat("Element at [1,2,3]:", arr[1, 2, 3], "\n")
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.