R Array

An array is an object helps in storing 3 dimensional data in R. When it is used to store 2 dimensional data becomes equivalent to matrix. When it is used to store 1 dimensional data, it becomes equivalent to vector. The first dimension in array is number of rows, second one is number of columns and third is number of matrices. So we can assume array as collection of matrices with similar number of rows and columns.

ARRAY CREATION METHOD

 ## Method-1 : It can be created by function, array(). It needs two input- data, dimensions of array which is passed in the form of vector. Dimnames is optional and is used for passing dimension names.
a<-array(1:40, c(2,2,10), dimnames=list(c("A","B"),c("Science","Maths"),c(2001:2010")))
a<-array(c(1,2,3,4,5,6,7,8) c(2,2,2))
 ## Method-2: It can be created by passing matrix.
m<-(1:8,4,2)
a<-array(m,c(2,2,2))
 ## Method-3: It can be created by changing dimensions of array,
v<-c(1,2,3,4,5,6,7,8,9,10,11,12)
dim(v)<-c(2,2,3)
 ## Method-4: It can be created by making blank array.
v<-array(,c(2,2,))

ARRAY ATTRIBUTES


a<-array(1:16,c(2,4,2))
dim(a)                                   ## return the dimensions of array
dim(a)<-c(2,2,8)                   ##update the dimensions of array
rownames<-c("Amy","Ben") ## update the rownames of an array
colnames<-c("A","B")           ##  update the colnames of an array
dimnames(a)<-list(c("A","B"),c("1","2"),2001:2008) ## update the names of all dimensions. i.e rownames, colnames, matrixnames

ARRAY OPERATIONS

a<-array(1:8,c(2,2,2))
b<-array(9:16,c(2,2,2))
a+b  ## Addition of two array
a-b    ## subtraction of two array
a*b   ## multiplication of two array
a/b    ## division of two array


No comments:

Post a Comment

Translate

Monte Carlo Simulation with R

Stochastic Modeling A stochastic model is a tool for modeling data where uncertainty is present with the input. When input has cert...