Arrays are the multidimensional collection of the elements have same data type
and can not mixed with other data type
Syntax : array( data , dim , dimnames )
data => data to store in the arrays
dim => ( no_of_rows , no_of_col , no_of_level)
no_of_level : it's means no. of matrix with same data and dimensions)
dimname => dimensions name
Example:
data = c('I' , 'Am' , 'Best')
dimension = c(3,3,3)
rows = c('R1' , 'R2' , 'R3' )
cols = c('C1' , 'C2' , 'C3' )
mat_name = c('Matrix -1' , 'Matrix - 2' , 'Matrix - 3')
ar = array(data , dim = dimension , dimnames = list(rows , cols , mat_name))
print(ar)
, , Matrix -1
C1 C2 C3
R1 "I" "I" "I"
R2 "Am" "Am" "Am"
R3 "Best" "Best" "Best"
, , Matrix - 2
C1 C2 C3
R1 "I" "I" "I"
R2 "Am" "Am" "Am"
R3 "Best" "Best" "Best"
, , Matrix - 3
C1 C2 C3
R1 "I" "I" "I"
R2 "Am" "Am" "Am"
R3 "Best" "Best" "Best"
C1 C2 C3
R1 "I" "I" "I"
R2 "Am" "Am" "Am"
R3 "Best" "Best" "Best"
, , Matrix - 2
C1 C2 C3
R1 "I" "I" "I"
R2 "Am" "Am" "Am"
R3 "Best" "Best" "Best"
, , Matrix - 3
C1 C2 C3
R1 "I" "I" "I"
R2 "Am" "Am" "Am"
R3 "Best" "Best" "Best"
Accessing the Array
# matrix_name [ Row_no , Col_no , Matrix_no]
Example:
ar[1,1,1]
ar[1,1,]
ar[1, , ] # or ar['R1' , , ] have same meaning
[1] "I"
Matrix -1 Matrix - 2 Matrix - 3
"I" "I" "I"
Matrix -1 Matrix - 2 Matrix - 3
C1 "I" "I" "I"
C2 "I" "I" "I"
C3 "I" "I" "I"
You may also interested in:
An Introduction to language R
Tutorial # 1.Data Type & Variable Declaration
Tutorial # 2.Operators
Tutorial # 3.Vector & Element Access
Tutorial # 4.Matrix
Tutorial # 5.Data Frame
Tutorial # 2.Operators
Tutorial # 3.Vector & Element Access
Tutorial # 4.Matrix
Tutorial # 5.Data Frame
Tutorial # 6.Arrays
Tutorial # 7.Lists
Tutorial # 8.Factors
Tutorial # 9.Data import
Tutorial # 10.Machine Learning
Tutorial # 11.Visualization
Tutorial # 7.Lists
Tutorial # 8.Factors
Tutorial # 9.Data import
Tutorial # 10.Machine Learning
Tutorial # 11.Visualization
0 Comments