R Arrays

🧊 R Arrays

Arrays in R are multi-dimensional data structures used to store data in more than two dimensions (2D, 3D, or more).

They are an extension of matrices and all elements must be of the same data type.


 1. What is an Array?

  • Can have 2 or more dimensions

  • Stores same type of data

  • Useful for scientific, mathematical, and statistical computations


 2. Creating an Array

Use the array() function.



 

📌 This creates a 2D array (3 rows × 4 columns)


 3. Creating a 3D Array



 

📌 Dimensions:

  • 2 rows

  • 3 columns

  • 2 matrices (layers)


 4. Accessing Array Elements

a) Access a single element



 

b) Access a full row



 

c) Access a full column



 

d) Access a full matrix (layer)



 


 5. Naming Dimensions


 


 6. Array Dimensions



 

✔ Output:

[1] 2 3 2

 7. Array Arithmetic (Element-wise)


 

✔ Operations apply element-wise


 8. Check if Object is Array



 


 9. Arrays vs Matrices

FeatureArrayMatrix
Dimensions2D or moreOnly 2D
Data typeSameSame
Use caseMultidimensional data2D calculations

 When to Use Arrays?

✔ Multidimensional datasets
✔ Scientific simulations
✔ Image & signal processing
✔ Advanced statistics


📌 Summary

  • Arrays store data in multiple dimensions

  • Created using array()

  • All elements must be same type

  • Access using [row, column, layer]

  • Extension of matrices

You may also like...