14 Feb R Arrays
To store data in more than two dimensions, we use Arrays in the R programming language. For this, the array() function is used. While creating an array, the dim parameter is used to set the array dimensions. In this lesson, we will learn how to create an Array and work on some operations on Arrays.
Create an Array
We will now create an array using the array() and dim. In the below example, we have taken values from 1 to 8. The dim is used to set the dimensions i.e. c(2,2,2). Let us see first what that means in the below figure:
Let us now see the example to create an array in the R programming language:
1 2 3 4 5 6 7 8 9 10 11 |
# An array with one dimension # The colon operator allows adding range of values # Value ranging from 1 to 8 oneDimArr <- c(1:8) oneDimArr # An array with more than one dimension multiDimArr <- array(oneDimArr, dim = c(2, 2, 2)) multiDimArr |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[1] 1 2 3 4 5 6 7 8 , , 1 [,1] [,2] [1,] 1 3 [2,] 2 4 , , 2 [,1] [,2] [1,] 5 7 [2,] 6 8 |
Iterate an Array
We can easily iterate through an array in R using the for loop. Let us see the example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# An array with one dimension # The colon operator allows adding range of values # Value ranging from 1 to 8 oneDimArr <- c(1:8) # An array with more than one dimension multiDimArr <- array(oneDimArr, dim = c(2, 2, 2)) # Iterate for(i in multiDimArr){ print(i) } |
Output
1 2 3 4 5 6 7 8 9 10 |
[1] 1 [1] 2 [1] 3 [1] 4 [1] 5 [1] 6 [1] 7 [1] 8 |
Find the length of an array
The length() function is used in R to get the length of the array. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# An array with one dimension # The colon operator allows adding range of values # Value ranging from 1 to 8 oneDimArr <- c(1:8) # Display oneDimArr # An array with more than one dimension multiDimArr <- array(oneDimArr, dim = c(2, 2, 2)) # Display multiDimArr # Ge the length length(multiDimArr) |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[1] 1 2 3 4 5 6 7 8 , , 1 [,1] [,2] [1,] 1 3 [2,] 2 4 , , 2 [,1] [,2] [1,] 5 7 [2,] 6 8 [1] 8 |
Above, the length of the array is 8.
Get the Number of rows and columns
The dim() function is used in R to get the number of rows and columns of an array. Let us see an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# An array with one dimension # The colon operator allows adding range of values # Value ranging from 1 to 8 oneDimArr <- c(1:8) # An array with more than one dimension multiDimArr <- array(oneDimArr, dim = c(2, 2, 2)) # Iterate for(i in multiDimArr){ print(i) } # Number of rows and columns dim(multiDimArr) |
Output
1 2 3 4 5 6 7 8 9 10 11 |
[1] 1 [1] 2 [1] 3 [1] 4 [1] 5 [1] 6 [1] 7 [1] 8 [1] 2 2 2 |
Access array items
Use the [] brackets to access array items i.e. elements of an R array. Just set the position number of the element you want to fetch. Follow the below syntax:
1 2 3 |
[row, column, level] |
Above, the row is the position number of the rows, the column is the number of columns, and the level is the level of the matrix i.e. the 1st, 2nd, and 3rd matrix.
In the below example, we will access the items with:
- row position 2, column position 2, and accessing the 1st matrix
- row position 1, column position 1, and accessing the 2nd matrix:
Let us see the example to access the array items in R:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# An array with one dimension # The colon operator allows adding range of values # Value ranging from 1 to 8 oneDimArr <- c(1:8) # An array with more than one dimension multiDimArr <- array(oneDimArr, dim = c(2, 2, 2)) # Display multiDimArr # Access items # row position is 2 # column position is 2 # matrix level is i.e. the 1st matrix multiDimArr[2, 2, 1] # row position is 1 # column position is 1 # matrix level is i.e. the 2nd matrix multiDimArr[1, 1, 2] |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
, , 1 [,1] [,2] [1,] 1 3 [2,] 2 4 , , 2 [,1] [,2] [1,] 5 7 [2,] 6 8 [1] 4 [1] 5 |
Find if a specific item is present in an array
Use the %in% operator in the R programming language to find if an item i.e. element is in an array. If the item is present, TRUE is returned, else FALSE.
Let us see an example to find items in an array:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# An array with one dimension # The colon operator allows adding range of values # Value ranging from 1 to 8 oneDimArr <- c(1:8) # An array with more than one dimension multiDimArr <- array(oneDimArr, dim = c(2, 2, 2)) # Display multiDimArr # Find specific items in the array using the %n% operator 7 %in% multiDimArr # Item is there 20 %in% multiDimArr # Item isn't there |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
, , 1 [,1] [,2] [1,] 1 3 [2,] 2 4 , , 2 [,1] [,2] [1,] 5 7 [2,] 6 8 [1] TRUE [1] FALSE |
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
No Comments