Explain the concept of 'Array' in 'R' language

 Posted by Niladri.Biswas on 5/1/2013 | Category: Others Interview questions | Views: 1354 | Points: 40
Answer:

Arrays are similar to matrices but can have more than two dimensions. They’re created with an array function of the following form:

myarray <- array(vector, dimensions, dimnames)


where
- vector contains the data for the array
- dimensions is a numeric vector giving the maximal index for each dimension
- dimnames is an optional list of dimension labels.

The following is an example of creating a three-dimensional (2x3x4) array of numbers.

> dim1 <- c("A1", "A2")

> dim2 <- c("B1", "B2", "B3")
> dim3 <- c("C1", "C2", "C3", "C4")
> z <- array(1:24, c(2, 3, 4), dimnames=list(dim1, dim2, dim3))
> z
, , C1
B1 B2 B3
A1 1 3 5
A2 2 4 6
, , C2
B1 B2 B3
A1 7 9 11
A2 8 10 12
, , C3
B1 B2 B3
A1 13 15 17
A2 14 16 18
, , C4
B1 B2 B3
A1 19 21 23
A2 20 22 24


Asked In: Many Interviews | Alert Moderator 

Comments or Responses

Login to post response