R Programming language - R Arrays

In R programming language, an array is a multi-dimensional data structure that can store values of the same data type. An array can have one or more dimensions, and each dimension is indexed by a set of integers. Here are some examples of arrays in R:

ref‮ re‬to:theitroad.com
array1 <- array(c(1, 2, 3, 4, 5, 6), dim = c(2, 3))
array2 <- array(1:24, dim = c(2, 3, 4))

In R, you can create an array using the array() function, which takes two arguments: the values to be stored in the array, and the dimensions of the array. For example, array1 is a two-dimensional array with dimensions 2 by 3, and array2 is a three-dimensional array with dimensions 2 by 3 by 4.

You can access individual elements of an array using square bracket notation, like this:

array1 <- array(c(1, 2, 3, 4, 5, 6), dim = c(2, 3))
print(array1[1, 2]) # Output: 2

In this case, array1[1, 2] refers to the element in the first row and second column of the array.

You can also access entire rows or columns of an array using square bracket notation, like this:

array1 <- array(c(1, 2, 3, 4, 5, 6), dim = c(2, 3))
print(array1[, 2]) # Output: 2 5

In this case, array1[, 2] refers to the entire second column of the array.

You can modify individual elements of an array using square bracket notation and the assignment operator, like this:

array1 <- array(c(1, 2, 3, 4, 5, 6), dim = c(2, 3))
array1[1, 2] <- 7
print(array1[1, 2]) # Output: 7

You can also add dimensions to an array using the dim() function, like this:

array1 <- array(c(1, 2, 3, 4, 5, 6), dim = c(2, 3))
dim(array1) <- c(3, 2, 1)

In this case, array1 is transformed from a two-dimensional array into a three-dimensional array with dimensions 3 by 2 by 1.

Understanding how to work with arrays in R is important for many data manipulation and analysis tasks, especially when working with large datasets that require multi-dimensional storage and analysis.