Chapter 7 R scalars and vectors

(Types of R objects: scalars, vectors and matrices)

7.1 Overview

7.1.1 Abstract:

Introduction to vector objects in R: what are they, how are they created, how can they be subset?

7.1.2 Objectives:

This unit will:

  • introduce scalars, vectors and matrices;
  • demonstrate vectorized operations;
  • teach various ways of subsetting;

7.1.3 Outcomes:

After working through this unit you:

  • can create vectors by assignment from sequences or using the c();
  • are familar with subsetting by index, name, and boolean vectors;
  • can subset elements, ranges, and slices from vectors and matrices;
  • can combine objects with c(), rbind(), or cbind().

7.1.4 Deliverables:

Time management: Before you begin, estimate how long it will take you to complete this unit. Then, record in your course journal: the number of hours you estimated, the number of hours you worked on the unit, and the amount of time that passed between start and completion of this unit.

Journal: Document your progress in your Course Journal. Some tasks may ask you to include specific items in your journal. Don't overlook these.

Insights: If you find something particularly noteworthy about this unit, make a note in your insights! page.

7.2 R Data types

R objects can be composed of different kinds of data according to the type and number of "atomic" values they contain:

  • Scalar items are single values;
  • Vectors are ordered sequences of scalars, they must all have the same "data type" (e.g. numeric, logical, character ...);
  • Matrices are vectors for which one or more "dimension(s)" have been defined;
  • "Data frames"" are spreadsheet-like objects, their columns are like vectors and all columns must have the same length, but within one data frame, columns can have different data types. They are the most commonly used type of object to hold data;
  • Lists are the most general collection of data items, the can contain items of any type and kind, including matrices, functions, data frames, and other lists.

7.3 Scalar data

Scalars are single numbers, the "atomic" parts of more complex datatypes. Of course we can work with single numbers in R, but under the hood they are actually vectors of length 1. (More on vectors in the next section). To create a scalar object, simply assign some value to its name.

define a scalar by assignment

x <- pi   

its value is ...

x   
## [1] 3.141593

its length is ...

length(x) 
## [1] 1

it is actually a vector, and its first element is ...

x[1]    
## [1] 3.141593

a second element does not exist NA: Not Available

x[2]        
## [1] NA

Here are some remarks on the types of scalars R uses, and on coercion between types, i.e. casting one datatype into another. The following scalar types are supported:

  • Boolean constants: TRUE and FALSE. This type has the "mode" logical;
  • Integers, floats (floating point numbers) and complex numbers. These types have the mode numeric;
  • Strings. These have the mode character.
  • Other modes exist, such as list, function and expression, all of which can be combined into complex objects.

The function mode() returns the mode of an object and typeof() returns its type. Also class() tells you what class it belongs to.

typeof(TRUE)
## [1] "logical"
class(3L)
## [1] "integer"
mode(print)
## [1] "function"

I have combined these information functions into a single function, objectInfo() which gets loaded and defined when you execute the init() function of the BasicSetup project, so you can explore objects in more detail. We can use objectInfo() to explore how R objects are made up, by handing various expressions as arguments to the function. Many of these you may not yet recognize ... bear with it though:

7.4 Task 13 - scalars

Load the R-Exercise_BasicSetup project in RStudio if you don't already have it open. Type init() as instructed after the project has loaded. Continue below.

Let's have a brief look at the function itself: typing a function name without its parentheses returns the source code for the function:

objectInfo
## function(x) {
##     # Function to combine various information items about R objects
##     #
##     # Input: an R object
##     # Value: none - prints information as side-effect
## 
##     cat("object contents:")
##     print(x, digits = 22)  # print value at maximal precision
## 
##     cat("\nstructure of object:\n")
##     str(x)
## 
##     if (! is.list(x)) { # Don't use cat() if x is a list. cat() can't handle lists.
##         cat("\nmode:   ", mode(x), "\n")
##         cat("typeof: ", typeof(x), "\n")
##         cat("class:  ", class(x), "\n")
##     }
## 
##     # if the object has attributes, print them too
##     if (! is.null(attributes(x))) {
##         cat("\nattributes:\n")
##         attributes(x)
##     }
##     # Done
## }

Try out the below

# Various objects:
 
#Scalars:
objectInfo( 3.0 )    # Double precision floating point number
objectInfo( 3.0e0 )  # Same value, exponential notation
 
objectInfo( 3 )   # Note: integers are double precision floats by default.
objectInfo( 3L )  # If we really want an integer, we must use R's
                  # special integer notation ...
objectInfo( as.integer(3) )  # or explicitly "coerce" to type integer...
 
# Coercions: For each of these, first think what result you would expect:
objectInfo( as.character(3) )  # Forcing the number to be interpreted as a character.
objectInfo( as.numeric("3") )   # character as numeric
objectInfo( as.numeric("3.141592653") )  # string as numeric. Where do the
                                         # non-zero digits at the end come from?
objectInfo( as.numeric(pi) )    # not a string, but a predefined constant
objectInfo( as.numeric("pi") )  # another string as numeric ... Ooops -
                                # why the warning?
objectInfo( as.complex(1) )
 
objectInfo( as.logical(0) )
objectInfo( as.logical(1) )
objectInfo( as.logical(-1) )
objectInfo( as.logical(pi) )      # any non-zero number is TRUE ...
objectInfo( as.logical("pie") )   # ... but not non-numeric types.
                                  # NA means "Not Available".
objectInfo( as.character(pi) )    # Interesting: the conversion eats digits.
 
objectInfo( Inf )                # Larger than the largest representable number
objectInfo( -Inf )               # ... or smaller
objectInfo( NaN )                # "Not a Number" is numeric
objectInfo( NA )                 # "Not Available" - i.e. missing value is
                                 # logical by default
 
# NULL
objectInfo( NULL )  # NULL is nothing. Not 0, not NaN,
                    # not FALSE - nothing. NULL is the value that is returned
                    # by expressions or functions when the result is
                    # undefined and nothing more can be said about it.
 
objectInfo( as.factor("M") )     # factor
objectInfo( Sys.time() )         # time
objectInfo( letters )            # inbuilt character vector
objectInfo( LETTERS )            # same
objectInfo( 1:4 )                # numeric vector
objectInfo( matrix(1:4, nrow=2)) # numeric matrix
objectInfo( data.frame(arabic = 1:3,                           # data frame
                       roman = c("I", "II", "III"),
                       stringsAsFactors = FALSE))
objectInfo( list(arabic = 1:7,
                 roman = c("I", "II", "III"),
                 chinese = c("一", "二", "三", "四")))   # list
 
# Expressions:
objectInfo( 3 > 5 ) # Note: any combination of two variables via the logical
                    # operators ! == != > < >= <= | || & and && is a
                    # logical expression, and evaluates to TRUE or FALSE.
objectInfo( 3 < 5 )
objectInfo( 1:6 > 4 ) # these are "vectorized" operators
 
objectInfo( a ~ b )              # a formula
objectInfo( objectInfo )         # the function itself

Sometimes (but rarely) you may run into a distinction that R makes regarding integers and floating point numbers. By default, if you specify e.g. the number 2 in your code, it is stored as a floating point number. But if the numbers are generated e.g. from a range operator as in 1:2 they are integers! This can give rise to confusion as in the following example:

a <- 7
b <- 6:7
str(a)             # num 7
##  num 7
str(b)             # int [1:2] 6 7
##  int [1:2] 6 7
a == b[2]          # TRUE
## [1] TRUE
identical(b[2], a) # FALSE ! Not identical! Why?
## [1] FALSE
                   # (see the str() results above.)
# If you need to be sure that a number is an
# integer, write it with an "L" after the number:
c <- 7L
str(c)             # int 7
##  int 7
identical(b[2], c) # TRUE
## [1] TRUE

7.5 Vectors

Since we (almost) never do statistics on scalars, R obviously needs ways to handle collections of data items. In its simplest form such a collection is a vector: an ordered list of items of the same type. * Vectors are created from scratch with the c() function which concatenates individual items into a vector, or with various sequencing functions. * Vectors have properties, such as length; * individual items in vectors can be combined in useful ways. * It's worth repeating: all elements of a vector must be of the same type. If they are not, they are silently(!) coerced to the most general type (which is often character). (The actual hierarchy for coercion is raw < logical < integer < double < complex < character < list ).

The c() function concatenates elements into a vector

c(2, 4, 6)
## [1] 2 4 6

Create a vector and list its contents and length:

f <- c(1, 1, 3, 5, 8, 13, 21)
f
## [1]  1  1  3  5  8 13 21
length(f)
## [1] 7

Often, for teaching code, I want to demonstrate the contents of an object after assigning it. I can simply wrap the assignment into parentheses to achieve that.

Parentheses return the value of whatever they enclose. So ... assigns 17 to the variable "a". But this happens silently.

a <- 17

However, returns the result of the assignment. I will use this idiom often.

( a <- 17 )
## [1] 17
( myVec <- c(1, 1, 3, 5, 8, 13, 21, 34, 55, 89) )
##  [1]  1  1  3  5  8 13 21 34 55 89

7.5.1 Coercion:

all elements of vectors must be of the same type

# trying to get a vector with mixed types ...
(mixed_type <- c(1, 2.0, "3", TRUE))  
## [1] "1"    "2"    "3"    "TRUE"
class(mixed_type)
## [1] "character"

... shows that all elements are silently being coerced to character. The emphasis is on silently. This might be unexpected, for example if you are reading numeric data from a text-file into a vector but someone has entered a " " for a missing value ... then everything is characterified. Nasty.

There are various ways to subset (retrieve) specific values from a vector; this is important.

7.6 Subsetting by index

Extracting by index: * "1" is first element, not 0. * head() gets the first n elements * length() is the index of the last element. * tail() get the last n elements

myVec[1]        
## [1] 1
head(myVec, 1)   # same thing
## [1] 1
myVec[length(myVec)] 
## [1] 89
tail(myVec, 1)       # same thing
## [1] 89

With a vector of indices: * This is the range operator

1:4 
## [1] 1 2 3 4

using the range operator (it generates a sequence and returns it in a vector)

myVec[1:4] 
## [1] 1 1 3 5

same thing, backwards

myVec[4:1]
## [1] 5 3 1 1

The seq() function is a flexible, generic way to generate sequences

seq(from=2, to=6, by=2) 
## [1] 2 4 6
seq(2, 6, 2) # Same thing: arguments in default order
## [1] 2 4 6
myVec[seq(2, 6, 2)]
## [1]  1  5 13

since a scalar is a vector of length 1, does this work?

5[1]

Using an index vector with positive indices the elements of index vectors must be valid indices of the target vector. The index vector can be of any length.

a <- c(1, 3, 4, 1) 
myVec[a] # In this case, four elements are retrieved from f[]
## [1] 1 3 5 1

7.7 Excluding items through negative indexes

Negative indices omit elements:

  • using an index vector with negative indices
# If elements of index vectors are negative integers,
# the corresponding elements are excluded.
( a <- -(1:4) ) # Note that this is NOT the same as -1:4
## [1] -1 -2 -3 -4
( a <- -(1:4) ) 
## [1] -1 -2 -3 -4
myVec[a] # Here, the first four elements are omitted from myVec[]
## [1]  8 13 21 34 55 89
myVec[-((length(myVec)-3):length(myVec))] # Here, the last four elements are omitted
## [1]  1  1  3  5  8 13

7.8 Subsetting by boolean vectors

A logical expression operating on the target vector returns a vector of logical elements. It has the same length as the target vector.

myVec > 4         
##  [1] FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE

We can use this logical vector to extract only elements for which the logical expression evaluates as TRUE. This is also called "filtering".

myVec[myVec > 4];  
## [1]  5  8 13 21 34 55 89

Note: the logical vector is aligned with the elements of the original vector. You can't retrieve elements more than once, as you could with index vectors. If the logical vector is shorter than its target it is "recycled" to the full length.

(1:20)[c(TRUE, FALSE)]  # odd numbers, but how and why?
##  [1]  1  3  5  7  9 11 13 15 17 19

7.9 Subsetting by name

If the vector has named elements, vectors of names can be used exactly like index vectors:

summary(myVec)["Median"]
## Median 
##   10.5
summary(myVec)[c("Max", "Min")]  # Oooops - I mistyped. But you can fix the expression, right?
## <NA> <NA> 
## 

7.10 "[" is an operator

Some more thoughts about "["

  • "[" is not just a special character, it is an operator.
  • It operates on whatever it is attached to on the left.
?"["   # help is available ...

We have attached "[" to vectors above, but we can also attach it directly to functions or other expressions.

For example, the summary() function returns some basic statistics of a vector:

summary(myVec)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    1.00    3.50   10.50   23.00   30.75   89.00

This is a vector of six numbers:

length(summary(myVec))
## [1] 6

We can extract e.g. the median like so:

summary(myVec)[3]
## Median 
##   10.5

... or the boundaries of the interquartile range:

summary(myVec)[c(2, 5)]
## 1st Qu. 3rd Qu. 
##    3.50   30.75

Note that the elements that summary() returns are "named". "Names" are attributes.

objectInfo(summary(f))
## object contents:                    Min.                  1st Qu.                   Median 
##  1.000000000000000000000  2.000000000000000000000  5.000000000000000000000 
##                     Mean                  3rd Qu.                     Max. 
##  7.428570999999999813213 10.500000000000000000000 21.000000000000000000000 
## 
## structure of object:
##  'summaryDefault' Named num [1:6] 1 2 5 7.43 10.5 ...
##  - attr(*, "names")= chr [1:6] "Min." "1st Qu." "Median" "Mean" ...
## 
## mode:    numeric 
## typeof:  double 
## class:   summaryDefault table 
## 
## attributes:
## $names
## [1] "Min."    "1st Qu." "Median"  "Mean"    "3rd Qu." "Max."   
## 
## $class
## [1] "summaryDefault" "table"

The names() function can retrieve (or set) names:

names(summary(myVec))
## [1] "Min."    "1st Qu." "Median"  "Mean"    "3rd Qu." "Max."

... which brings us to yet another way to extract elements from Vectors:

7.11 Subsetting by name:

Extending vectors:

  • Vectors are not immutable. They can grow and shrink as required.
( x <- 1:3 )
## [1] 1 2 3
length(x)
## [1] 3
x[4] <- 4; x
## [1] 1 2 3 4
length(x)
## [1] 4

You can add to any index you would like. R will fill the intermediary spots with NA

x[7] <- 7; x
## [1]  1  2  3  4 NA NA  7
length(x)
## [1] 7
( x <- x[-(5:6)] )
## [1] 1 2 3 4 7
length(x)
## [1] 5

Example : extending the Fibonacci series for three steps.

( myVec <- c(myVec, myVec[length(myVec)-1] + myVec[length(myVec)]) )
##  [1]   1   1   3   5   8  13  21  34  55  89 144
( myVec <- c(myVec, myVec[length(myVec)-1] + myVec[length(myVec)]) )
##  [1]   1   1   3   5   8  13  21  34  55  89 144 233
( myVec <- c(myVec, myVec[length(myVec)-1] + myVec[length(myVec)]) )
##  [1]   1   1   3   5   8  13  21  34  55  89 144 233 377

7.12 Task 14 - vectors

Think: How does this work? What numbers are we adding here and why does the result end up in the vector?

7.13 Vectorized operations

Many operations on vectors are by default performed for every element of the vector, and R computes them very efficiently. These are called "vectorized" operations and definitely should be used whenever possible, rather than loops or other explicit iterations.

For example:

myVec
##  [1]   1   1   3   5   8  13  21  34  55  89 144 233 377
myVec + 1
##  [1]   2   2   4   6   9  14  22  35  56  90 145 234 378
log(myVec)
##  [1] 0.000000 0.000000 1.098612 1.609438 2.079442 2.564949 3.044522 3.526361
##  [9] 4.007333 4.488636 4.969813 5.451038 5.932245
myVec * 2
##  [1]   2   2   6  10  16  26  42  68 110 178 288 466 754

7.14 computing with two vectors of same length

the Fibonacci numbers you have defined above

myVec 
##  [1]   1   1   3   5   8  13  21  34  55  89 144 233 377

like myVec, but omitting the first element

( a <- myVec[-1] )
##  [1]   1   3   5   8  13  21  34  55  89 144 233 377

like f, but shortened by the last element

( b <- myVec[1:(length(myVec)-1)] )
##  [1]   1   1   3   5   8  13  21  34  55  89 144 233

the "golden ratio", phi (~1.61803 or (1+sqrt(5))/2 ),an irrational number, is approximated by the ratio of two consecutive Fibonacci numbers.

c <- a / b
abs(c - ((1+sqrt(5))/2)) # Calculating the error of the approximation, element by element
##  [1] 6.180340e-01 1.381966e+00 4.863268e-02 1.803399e-02 6.966011e-03
##  [6] 2.649373e-03 1.013630e-03 3.869299e-04 1.478294e-04 5.646066e-05
## [11] 2.156681e-05 8.237677e-06

When a number is not a single number:

One of the "warts" of R is that some functions substitute a range when they receive a vector of length one. Most everyone agrees this is pretty bad. This behaviour was introduced when someone sometime long ago thought it would be nifty to save two keystrokes. This has caused countless errors, hours of frustration and probably hundreds of undiscovered bugs instead. Today we wouldn't write code like that anymore (I hope), but the community believes that since it's been around for so long, it would probably break more things if it's changed. Two functions to watch out for are sample() and seq(); other functions include diag() and runif(). Consider: x <- 8; sample(6:x) x <- 7; sample(6:x) x <- 6; sample(6:x) # Oi!

also consider

x <- 6:8; seq(x) x <- 6:7; seq(x) x <- 6:6; seq(x) # Oi vay!

Wherever this misbehaviour is a possibility - i.e. when the number of elements to sample from is variable and could be just one, for example in some simulation code - you can write a replacement function like so... safeSample <- function(x, size, ...) { # Replace the sample() function to ensure sampling from a single # value returns that value. # Respect additional arguments if present. if (length(x) == 1 && is.numeric(x) && x > 0) { if (missing(size)) size <- 1 return(rep(x, size)) } else { return(sample(x, size, ...)) } }

Don't be discouraged though: such warts are rare in R.

7.15 Matrices and higher-dimensional objects

If we need to operate with several vectors, or multi-dimensional data, we make use of matrices or more generally k-dimensional arrays. Matrix operations are very similar to vector operations, in fact a matrix actually is a vector for which the number of rows and columns have been defined. Thus matrices inherit the basic limitation of vectors: all elements have to be of the same type.

The most basic way to define matrix rows and columns is to use the dim() function and specify the size of each dimension. Consider:

( a <- 1:12 )
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12
dim(a) <- c(2,6)
a
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    3    5    7    9   11
## [2,]    2    4    6    8   10   12
dim(a) <- c(4,3)
a
##      [,1] [,2] [,3]
## [1,]    1    5    9
## [2,]    2    6   10
## [3,]    3    7   11
## [4,]    4    8   12
dim(a) <- c(2,2,3)
a
## , , 1
## 
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
## 
## , , 2
## 
##      [,1] [,2]
## [1,]    5    7
## [2,]    6    8
## 
## , , 3
## 
##      [,1] [,2]
## [1,]    9   11
## [2,]   10   12

dim() also tells you the number of rows resp. columns a matrix has. For example:

  • dim(a) # returns the dimensions of a in a vector
  • dim(a)[3] # only the size of the third dimension of a

If you have a two-dimensional matrix, the function nrow() and ncol() will also give you the number of rows and columns, respectively. Obviously, dim(a)[1] is the same as nrow(a).

As an alternative to dim(), matrices can be defined using the matrix() or array() functions (see there), or "glued" together from vectors by rows or columns, using the rbind() or cbind() functions respectively:

( a  <- 1:4 )
## [1] 1 2 3 4
( b  <- 5:8 )
## [1] 5 6 7 8
( m1 <- rbind(a, b) )
##   [,1] [,2] [,3] [,4]
## a    1    2    3    4
## b    5    6    7    8
( m2 <- cbind(a, b) )
##      a b
## [1,] 1 5
## [2,] 2 6
## [3,] 3 7
## [4,] 4 8
( m  <- cbind(m2, c = 9:12) )  # naming a column "c" while cbind()'ing it
##      a b  c
## [1,] 1 5  9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12

"Subsetting" (retrieving) individual elements or slices from matrices is simply done by specifying the appropriate indices, where a missing index indicates that the entire row or column is to be retrieved.

7.16 Task 15 - matrices

Explore how you extract rows or columns from a matrix by specifying them. Within the square brackets the order is [, <columns<]

  • m[1,] # first row
  • m[, 2] # second column
  • m[3, 2] # element at row == 3, column == 2
  • m[3:4, 1:2] # submatrix: rows 3 to 4 and columns 1 to 2

Note that R has numerous functions to compute with matrices, such as transposition, multiplication, inversion, calculating eigenvalues and eigenvectors and much more.

7.17 Self-evaluation

  • Question 1 : Within the square brackets of a matrix the order is [, ], but what about slices of a 3D matrix? Is it [, , ] or [, , ]?

  • Answer: Just try -15

x <- 1:27
dim(x) <- c(3, 3, 3)
x
x[2, 1, 1]   # 2
x[1, 1, 2]   # 10

  1. and this means [, ] is correct.