R Programming में arrays ये matrices जैसे होते है लेकिन matrices ये two-dimensional होते है और arrays ये दो से ज्यादा dimensions पर sequence या vector पर दिए हुए elements को store कर सकते है |
R में array को create करने के लिए array() function का इस्तेमाल किया जाता है |
Syntax for array() Function R
array( sequence/vector, dim=(no_of_rows, no_of_columns, no_of_matrix), dimnames=list(rownames, colnames, matrixnames) )
Parameters for array() Function in R
sequence/vector : यहाँ पर sequence या vector दिया जाता है |
dim : Optional. dim parameter की value पर create किये जाने वाले rows की संख्या , columns की संख्या और matrices की संख्या दी जाती है |
dimnames : Optional. dimnames parameter पर rows, columns और matrices के लिए नाम दिए जाते है |
Simple Example for array() Function in R
Example पर vector पर दिए हुए elements के 2 rows और 4 columns के 2 matrices create किये गए है |
Source Code :> array(c(1, 2, 3, 4, 2, 5, 4, 4), dim = c(2,4,2)) , , 1 [,1] [,2] [,3] [,4] [1,] 1 3 2 4 [2,] 2 4 5 4 , , 2 [,1] [,2] [,3] [,4] [1,] 1 3 2 4 [2,] 2 4 5 4
Example for array() Function with dimnames(Naming rows, columns and matrices) in R
Example पर rows, columns और matrices को dimnames इस parameter की मदद से नाम दिए गए है |
Source Code :> rownames = c('row1', 'row2') > colnames = c('col1', 'col2', 'col3', 'col4') > matnames = c('matrix1', 'matrix2') > array(c(1, 2, 3, 4, 2, 5, 4, 4), + dim = c(2, 4, 2), + dimnames = list(rownames, colnames, matnames)) , , matrix1 col1 col2 col3 col4 row1 1 3 2 4 row2 2 4 5 4 , , matrix2 col1 col2 col3 col4 row1 1 3 2 4 row2 2 4 5 4
Example for Accessing Array Elements in R
Example पर पहले statement में 2nd matrix के 2nd row और 4th column के element को access किया गया है |
दुसरे statement में 2nd matrix के पूरे 2nd row को access किया गया है |
Source Code :> arr = array(c(1,2,3,4, 2, 5, 4, 4), dim = c(2,4,2)) > arr[2, 4, 2] #Respectively [2nd row, 4th column, 2nd matrix] [1] 4 > arr[2, , 2] #Respectively [2nd row, , 2nd matrix] [1] 2 4 5 4