1 Problem 1: The seq() function

  1. The starting point is “from”, and “to” is the end. Increment is “by”, and “length.out” is the desired length.
x <- seq(from=1, to=10, by=2)
x
## [1] 1 3 5 7 9
seq(10, 1, length.out=5)
## [1] 10.00  7.75  5.50  3.25  1.00
seq(10, 1, along.with=x)
## [1] 10.00  7.75  5.50  3.25  1.00
  1. If “to” is not specified, then the sequence length is 1:from. If “by” is not specified, the defaut value is 1.
seq(10)
##  [1]  1  2  3  4  5  6  7  8  9 10
seq(3,10)
## [1]  3  4  5  6  7  8  9 10
  1. When you pass seq() a vector, it returns a sequence of same length, starting from 1 and increment by 1.
seq(along.with=x)
## [1] 1 2 3 4 5
  1. If the vector is of length 1, it is interpreted as 1:from, instead of “along.with”.
y <- seq.int(3,3,1)
y
## [1] 3
seq(y)
## [1] 1 2 3

5.It is preferable to use seq_along() in this case.

seq_along(x)
## [1] 1 2 3 4 5
seq_along(y)
## [1] 1
z <- as.integer(seq_len(8)+2)
z
## [1]  3  4  5  6  7  8  9 10
typeof(z)
## [1] "integer"
  1. For positive case it is 1:num. For negative case it is error.
seq_len(8)
## [1] 1 2 3 4 5 6 7 8
#seq_len(-8)

2 Problem 2: Vectors

letters
##  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q"
## [18] "r" "s" "t" "u" "v" "w" "x" "y" "z"
summary(letters)
##    Length     Class      Mode 
##        26 character character
typeof(letters)
## [1] "character"
len <- length(letters)
len
## [1] 26
alterletter <- letters[seq(1,len,2)]
alterletter
##  [1] "a" "c" "e" "g" "i" "k" "m" "o" "q" "s" "u" "w" "y"
backletter <- letters[seq(len,1)]
backletter
##  [1] "z" "y" "x" "w" "v" "u" "t" "s" "r" "q" "p" "o" "n" "m" "l" "k" "j"
## [18] "i" "h" "g" "f" "e" "d" "c" "b" "a"
alterback <- letters[seq(len,1,-2)]
alterback
##  [1] "z" "x" "v" "t" "r" "p" "n" "l" "j" "h" "f" "d" "b"
matrix(letters[1:16],4,4)
##      [,1] [,2] [,3] [,4]
## [1,] "a"  "e"  "i"  "m" 
## [2,] "b"  "f"  "j"  "n" 
## [3,] "c"  "g"  "k"  "o" 
## [4,] "d"  "h"  "l"  "p"
  1. Built-in Constants are LETTERS, letters, month.abb, month.name, pi.
c(typeof(month.abb),typeof(month.name),typeof(pi))
## [1] "character" "character" "double"

3 Problem 3: Matrices

m <- matrix(runif(20),4,5)
  1. It is an array, and it is not an 1 dimension matrix. There is no dim attribute.
m[3,]
## [1] 0.3224788 0.3042040 0.6357695 0.2075427 0.6186550
dim(m[3,])
## NULL
  1. In this way, the result is not coerced to the lowest possible dimension, which means it is still a matrix.
m[3,,drop=FALSE]
##           [,1]     [,2]      [,3]      [,4]     [,5]
## [1,] 0.3224788 0.304204 0.6357695 0.2075427 0.618655
dim(m[3,,drop=FALSE])
## [1] 1 5
sum(m)
## [1] 11.70932
m <- m/sum(m)
sum(m)
## [1] 1
rowSums(m)
## [1] 0.2811292 0.3185742 0.1783750 0.2219216
colSums(m)
## [1] 0.2120096 0.1360472 0.2385375 0.1754245 0.2379813
m <- m/rowSums(m)
rowSums(m)
## [1] 1 1 1 1

8.Transpose.

dim(t(m))
## [1] 5 4
m <- t(t(m)/colSums(m))
colSums(m)
## [1] 1 1 1 1 1
  1. First every other element mving along the columns, then rows.
m2 <- matrix(runif(2*9),2,9)
m2
##            [,1]      [,2]      [,3]      [,4]      [,5]       [,6]
## [1,] 0.17236332 0.3333213 0.7547162 0.2958103 0.1538531 0.05770839
## [2,] 0.02093003 0.2480183 0.5950389 0.3845904 0.7293498 0.44559583
##           [,7]      [,8]      [,9]
## [1,] 0.6793890 0.7347824 0.2003478
## [2,] 0.1494083 0.3806349 0.6439798
m2[seq(1,18,2)]
## [1] 0.17236332 0.33332125 0.75471616 0.29581033 0.15385313 0.05770839
## [7] 0.67938898 0.73478245 0.20034783
t(m2)[seq(1,18,2)]
## [1] 0.1723633 0.7547162 0.1538531 0.6793890 0.2003478 0.2480183 0.3845904
## [8] 0.4455958 0.3806349
  1. A vector of “1” and vector 2 are column bind to be a matrix. This matrix is then column bind with vector 3. However, by matrix indexing, vector 3 is put in between vector 1 and vector 2.
m <- cbind(1, 1:7) 
# the ’1’ (= shorter vector) is recycled
m
##      [,1] [,2]
## [1,]    1    1
## [2,]    1    2
## [3,]    1    3
## [4,]    1    4
## [5,]    1    5
## [6,]    1    6
## [7,]    1    7
m <- cbind(m, 8:14)[, c(1, 3, 2)] 
# insert a column
m
##      [,1] [,2] [,3]
## [1,]    1    8    1
## [2,]    1    9    2
## [3,]    1   10    3
## [4,]    1   11    4
## [5,]    1   12    5
## [6,]    1   13    6
## [7,]    1   14    7