Database Reference
In-Depth Information
the number of elements is not initially known and the new elements will later be
added to the end of the vector as the values become available.
a <- vector(length=3)
# create a logical vector of length
3
a # returns FALSE FALSE FALSE
b <- vector(mode="numeric", 3) # create a numeric vector of
length 3
typeof(b) # returns "double"
b[2] <- 3.1 # assign 3.1 to the 2nd element
b # returns 0.0 3.1 0.0
c <- vector(mode="integer", 0) # create an integer vector
of length 0
c
# returns integer(0)
length(c)
# returns 0
Although vectors may appear to be analogous to arrays of one dimension, they are
technically dimensionless, as seen in the following R code. The concept of arrays
and matrices is addressed in the following discussion.
length(b)
# returns 3
dim(b)
# returns NULL (an undefined value)
Arrays and Matrices
The array() function can be used to restructure a vector as an array. For
example, the following R code builds a three-dimensional array to hold the
quarterly sales for three regions over a two-year period and then assign the sales
amount of $158,000 to the second region for the first quarter of the first year.
# the dimensions are 3 regions, 4 quarters, and 2 years
quarterly_sales <- array(0, dim=c(3,4,2))
quarterly_sales[2,1,1] <- 158000
quarterly_sales
, , 1
[,1] [,2] [,3] [,4]
[1,] 0 0 0 0
[2,] 158000 0 0 0
[3,] 0 0 0 0
, , 2
Search WWH ::




Custom Search