R Arrays

  • An array is a collection of elements of the same data type.
  • An array is made up of three dimensions
  • The array() function is used to create arrays.
  • Syntax: array(data, dim = c(dim1, dim2, dim3, dimnames = NULL))

Syntax

  • data: a vector list
  • dim: the dimensions of the array in terms of dim1(length of rows) and dim2( length of columns) and dim3(number of layers)

Example 1

Example 2

Access Array Items

From the above, print(this_is_an_array_of_numbers[3, 2, 1])

  • The syntax is as follow: array[row position, column position, matrix level]
  • The above will print the item that is in the 3rd row, 2nd column and first dimension or layer = .8
  • An entire column may be accessed using the c() function

Example 2:

%in%: Check for the Existence of items in an array

To check if an items exists in a array, we can use the %in% operator

Loop Through an Array: For Loop

Let us create a an array, loop through it and see how it works

Length() array: Find the Length of an array

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top