This notebook strictly follows from Introduction to R.

2 Simple manipulations; numbers and vectors

2.1 Vectors and assignment

 c(10.4, 5.6, 3.1, 6.4, 21.7) -> x

2.2 Vector arithmetic

sqrt(-17)
## Warning in sqrt(-17): 产生了NaNs
## [1] NaN
sqrt(-17+0i)
## [1] 0+4.123106i
abs(1+1i)
## [1] 1.414214
Mod(complex(real=1,imaginary=1))
## [1] 1.414214

2.3 Generating regular sequences

n <- 5
1:n-1
## [1] 0 1 2 3 4
1:(n-1)
## [1] 1 2 3 4
 seq(-5, 5, by=1)
##  [1] -5 -4 -3 -2 -1  0  1  2  3  4  5
 seq(length=5, from=-5, by=.2)
## [1] -5.0 -4.8 -4.6 -4.4 -4.2
 seq(from=-5, along=c(1,4,7))
## [1] -5 -4 -3
rep(c(1,2), times=5)
##  [1] 1 2 1 2 1 2 1 2 1 2
rep(c(1,2), each=5)
##  [1] 1 1 1 1 1 2 2 2 2 2

2.4 Logical vectors

c(1, 0) == c(TRUE, FALSE)
## [1] TRUE TRUE

2.5 Missing values

NA == NA
## [1] NA
is.na(NA)
## [1] TRUE
is.na(NaN)
## [1] TRUE
is.nan(NA)
## [1] FALSE
is.nan(NaN)
## [1] TRUE

2.6 Character vectors

print("x max value")
## [1] "x max value"
 paste(c("X","Y"), 1:10, sep="")
##  [1] "X1"  "Y2"  "X3"  "Y4"  "X5"  "Y6"  "X7"  "Y8"  "X9"  "Y10"
 paste(c("X","Y"), 1:10, collapse="|")
## [1] "X 1|Y 2|X 3|Y 4|X 5|Y 6|X 7|Y 8|X 9|Y 10"

2.7 Index vectors; selecting and modifying subsets of a data set

x <- c(-2,-1,NA,2,3)
x[!is.na(x)]
## [1] -2 -1  2  3
(x+1)[(!is.na(x)) & x>0]
## [1] 3 4
c("x","y")[rep(c(1,2,2,3), times=2)]
## [1] "x" "y" "y" NA  "x" "y" "y" NA
x <- c(-2,-1,NA,2,3)
x[-(1:2)]
## [1] NA  2  3
fruit <- c(5, 10, 1, 20)
names(fruit) <- c("orange", "banana", "apple", "peach")
fruit[c("apple","orange")]
##  apple orange 
##      1      5

2.8 Other types of objects

3 Objects, their modes and attributes

3.1 Intrinsic attributes: mode and length

mode(x)
## [1] "numeric"
length(x)
## [1] 5
attributes(x)
## NULL

3.2 Changing the length of an object

e <- numeric()
e[10] <- 17
e[4] <- 17
e
##  [1] NA NA NA 17 NA NA NA NA NA 17
e <- e[2 * 1:5]
e
## [1] NA 17 NA NA 17
length(e) <- 3
e
## [1] NA 17 NA

3.3 Getting and setting attributes

x <- 1:9
attr(x,"dim") <- c(3, 3)
x
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9

3.4 The class of an object

A special attribute known as the class of the object is used to allow for an object-oriented style of programming in R. A different style using ‘formal’ or ‘S4’ classes is provided in package methods.

class(x)
## [1] "matrix"
f <- data.frame("first"=c(1,2,3),"second"=c(2,3,4))
f
mode(unclass(f))
## [1] "list"
unclass(f)
## $first
## [1] 1 2 3
## 
## $second
## [1] 2 3 4
## 
## attr(,"row.names")
## [1] 1 2 3

4 Ordered and unordered factors

4.1 A specific example

state <- c("tas", "sa", "qld", "nsw", "nsw", "nt", "wa", "wa")

fac <- factor(state)
fac
## [1] tas sa  qld nsw nsw nt  wa  wa 
## Levels: nsw nt qld sa tas wa
sort(state)
## [1] "nsw" "nsw" "nt"  "qld" "sa"  "tas" "wa"  "wa"
levels(factor(state))
## [1] "nsw" "nt"  "qld" "sa"  "tas" "wa"

4.2 The function tapply() and ragged arrays

incomes <- c(60, 49, 40, 61, 64, 60, 59, 54)
incmeans <- tapply(incomes, fac, mean)
incmeans
##  nsw   nt  qld   sa  tas   wa 
## 62.5 60.0 40.0 49.0 60.0 56.5
first <- function(x) x[1]
incfirs <- tapply(incomes, fac, first)
incfirs
## nsw  nt qld  sa tas  wa 
##  61  60  40  49  60  59

4.3 Ordered factors

ff <- factor(substring("statistics", 1:10, 1:10), levels = letters)
ff
##  [1] s t a t i s t i c s
## Levels: a b c d e f g h i j k l m n o p q r s t u v w x y z
as.integer(ff)
##  [1] 19 20  1 20  9 19 20  9  3 19
f. <- factor(ff)
f.
##  [1] s t a t i s t i c s
## Levels: a c i s t
ff[, drop = TRUE]
##  [1] s t a t i s t i c s
## Levels: a c i s t
ordered(4:1)
## [1] 4 3 2 1
## Levels: 1 < 2 < 3 < 4
class(ordered(4:1))
## [1] "ordered" "factor"
z <- factor(LETTERS[3:1], ordered = TRUE)
z
## [1] C B A
## Levels: A < B < C

5 Arrays and matrices

5.1 Arrays

5.2 Array indexing. Subsections of an array

5.3 Index matrices

5.4 The array() function

5.4.1 Mixed vector and array arithmetic. The recycling rule

5.5 The outer product of two arrays

d <- outer(0:10, 0:10)
fr <- table(outer(d, d, "-"))
plot(fr, xlab="Determinant", ylab="Frequency")

#### 5.6 Generalized transpose of an array

A <- matrix(1:4,2,2)
aperm(A, c(2,1)) == t(A)
##      [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE