Array in R programming language

  • Array are homogenous data structures (like vector, factor, matrix) and can contain items of similar class only.
  • n-dimensional.
  • Matrix can be considered as multiple sheets (matrices) stacked over each other.
  • We can use array() function to create an array.
    • First paramer to this array () function is combine function to combine two matrices.
    • Second parameter dimension or dim is set to an integer vector containing three integer values. First digit represents number of rows in sheet, second digit represents number of columns in sheet and third digit is the sheet number itself.

Script for Array in R

  1. class1.student.eng.marks <- c(61L, 62L, 63L, 65L) #integer vector
  2. class1.student.maths.marks <- c(71L, 72L, 73L, 75L) #integer vector
  3. class1.student.marks <- cbind (class1.student.eng.marks, class1.student.maths.marks) #column-wise matrix binding
  4. class1.student.marks #print content of created matrix (class1)
  5. class2.student.eng.marks <- c(66L, 67L, 68L, 69L) #integer vector
  6. class2.student.maths.marks <- c(76L, 77L, 78L, 79L) #integer vector
  7. class2.student.marks <- cbind (class1.student.eng.marks, class2.student.maths.marks) #column-wise matrix binding
  8. class2.student.marks #print content of created matrix (class2)
  9. class.marks <- array( c(class1.student.marks, class2.student.marks), dim= c(4,2,2)) #create array
  10. class.marks # print array (2 sheets)
  11. #Extracting elements from an array
  12. class.marks [1,2,2] # extracting single element from array (row, column, sheet)
  13. [1] 76
  14. class.marks [1:3,2,] #colon or sequence operator to extract rows in sequence (for all sheets)
  15. class.marks [c(1,3),1,1] #combine function to extract specific rows
  16. [1] 61 63