Previous: Arrays | Up: Arrays | Next: Lists |
> a <- c(3, 7, 9, 11) # Concatenates numeric values into a vector > a <- c("a", "b", "c") # Concatenates character strings into a vector > a <- 1:5 # Creates a vector of integers from 1 to 5 inclusive > a <- rep(1, times = 5) # Creates a vector of 5 repeated `1'sTo manipulate a vector:
> a[10] # Extracts the 10th value from the vector `a' > a[5] <- 3.14 # Inserts 3.14 as the 5th value in the vector `a' > a[5:7] <- c(2, 4, 7) # Replaces the 5th through 7th values with 2, 4, and 7Unlike larger arrays, vectors can be extended without first creating another vector of the correct length. Hence,
> a <- c(4, 6, 8) > a[5] <- 9 # Inserts a 9 in the 5th position of the vector, # automatically inserting an `NA' values position 4
> x <- c(1, 1, 1, 1, 1, 2, 2, 2, 2, 9, 9, 9, 9) > factor(x) [1] 1 1 1 1 1 2 2 2 2 9 9 9 9 Levels: 1 2 9By default, factor() creates unordered factors, which are treated as discrete, rather than ordered, levels. Add the optional argument ordered = TRUE to order the factors in the vector:
> x <- c("like", "dislike", "hate", "like", "don't know", "like", "dislike") > factor(x, levels = c("hate", "dislike", "like", "don't know"), + ordered = TRUE) [1] like dislike hate like don't know like dislike Levels: hate < dislike < like < don't knowThe factor() command orders the levels according to the order in the optional argument levels. If you omit the levels command, R will order the values as they occur in the vector. Thus, omitting the levels argument sorts the levels as like < dislike < hate < don't know in the example above. If you omit one or more of the levels in the list of levels, R returns levels values of NA for the missing level(s):
> factor(x, levels = c("hate", "dislike", "like"), ordered = TRUE) [1] like dislike hate like <NA> like dislike Levels: hate < dislike < likeUse factored vectors within data frames for plotting (see Section ), to set the values of the explanatory variables using setx (see Section ) and in the ordinal logit and multinomial logit models (see Section ).
> matrix(c(1,2,3,4,5,6), nrow = 2, ncol = 3) [,1] [,2] [,3] # Note that when assigning a vector to a [1,] 1 3 5 # matrix, none of the rows or columns [2,] 2 4 6 # have names.
> x <- c(11, 12, 13) # Creates a vector `x' of 3 values. > y <- c(55, 33, 12) # Creates another vector `y' of 3 values. > rbind(x, y) # Creates a 2 x 3 matrix. Note that row [,1] [,2] [,3] # 1 is named x and row 2 is named y, x 11 12 13 # according to the order in which the y 55 33 12 # arguments were passed to rbind(). > cbind(x, y) # Creates a 3 x 2 matrix. Note that the x y # columns are named according to the [1,] 11 55 # order in which they were passed to [2,] 12 33 # cbind(). [3,] 13 12
> loo[,3] # Extracts the third column of loo. > loo[1,] # Extracts the first row of loo. > loo[1,3] <- 13 # Inserts 13 as the value for row 1, column 3. > loo[1,] <- c(2,2,3) # Replaces the first row of loo.If you encounter problems replacing rows or columns, make sure that the dims() of the vector matches the dims() of the matrix you are trying to replace.
> a <- matrix(8, 2, 3) # Creates a 2 x 3 matrix populated with 8's. > b <- matrix(9, 2, 3) # Creates a 2 x 3 matrix populated with 9's. > array(c(a, b), c(2, 3, 2)) # Creates a 2 x 3 x 2 array with the first , , 1 # level [,,1] populated with matrix a (8's), # and the second level [,,2] populated [,1] [,2] [,3] # with matrix b (9's). [1,] 8 8 8 [2,] 8 8 8 # Use square brackets to extract values. For # example, [1, 2, 2] extracts the second , , 2 # value in the first row of the second level. # You may also use the <- operator to [,1] [,2] [,3] # replace values. [1,] 9 9 9 [2,] 9 9 9If an array is a one-dimensional vector or two-dimensional matrix, R will treat the array using the more specific method.
Three functions especially helpful for arrays:
> dims(b) [1] 33 5indicates that the array is two-dimensional (a matrix), and has 33 rows and 5 columns.
[ ]
indicates specific values in the
array. Use commas to indicate the index of the specific values you
would like to pull out or replace:
> dims(a) [1] 14 > a[10] # Pull out the 10th value in the vector `a' > dims(b) [1] 33 5 > b[1:12, ] # Pull out the first 12 rows of `b' > c[1, 2] # Pull out the value in the first row, second column of `c' > dims(d) [1] 1000 4 5 > d[ , 3, 1] # Pulls out a vector of 1,000 values