2+2
## [1] 4
14/6
## [1] 2.333333
10^2+3*60/8-3
## [1] 119.5
2^(2+1)-4+64^((-2)^(2.25-1/4))
## [1] 16777220
24321510142345456556566
## [1] 2.432151e+22
(6*2.3 +42)/3^(4.2-3.62)
## [1] 29.50556
(-4)^2 +2
## [1] 18
sqrt(25.2/2)
## [1] 3.549648
sqrt(15/2)
## [1] 2.738613
sqrt(16.44/2)
## [1] 2.867054
sqrt(15.3/2)
## [1] 2.765863
sqrt(18.6/2)
## [1] 3.04959
log(0.3)
## [1] -1.203973
exp(log(0.3))
## [1] 0.3
-0.00000000423546322
## [1] -4.235463e-09
(3^2)*(4^1/8)
## [1] 4.5
x <- 4.5
x = x/2.33  #this overwrites the previous value of x
x
## [1] 1.93133
-8.2*10^(-13)
## [1] -8.2e-13
y <- -8.2e-13
x*y
## [1] -1.583691e-12
myvec <- c(2,2,3,4,5,7)
myvec1 <- c(1,3,4,1,2,5)
myvec2 <- c(myvec,myvec1)
myvec2
##  [1] 2 2 3 4 5 7 1 3 4 1 2 5
seq(from=3,to=27,by=3)
## [1]  3  6  9 12 15 18 21 24 27
seq(from=3,to=27,length.out=40)
##  [1]  3.000000  3.615385  4.230769  4.846154  5.461538  6.076923  6.692308
##  [8]  7.307692  7.923077  8.538462  9.153846  9.769231 10.384615 11.000000
## [15] 11.615385 12.230769 12.846154 13.461538 14.076923 14.692308 15.307692
## [22] 15.923077 16.538462 17.153846 17.769231 18.384615 19.000000 19.615385
## [29] 20.230769 20.846154 21.461538 22.076923 22.692308 23.307692 23.923077
## [36] 24.538462 25.153846 25.769231 26.384615 27.000000
zoo <- 5.3
justseq <- seq(from=zoo,to=(-47+1.5),by=-2.4)
justseq
##  [1]   5.3   2.9   0.5  -1.9  -4.3  -6.7  -9.1 -11.5 -13.9 -16.3 -18.7 -21.1
## [13] -23.5 -25.9 -28.3 -30.7 -33.1 -35.5 -37.9 -40.3 -42.7 -45.1
rep(x=1,times=4)
## [1] 1 1 1 1
 rep(x=c(3,62,8.3),times=3)
## [1]  3.0 62.0  8.3  3.0 62.0  8.3  3.0 62.0  8.3
rep(x=c(3,62,8.3),each=2)
## [1]  3.0  3.0 62.0 62.0  8.3  8.3
 rep(x=c(3,62,8.3))
## [1]  3.0 62.0  8.3
 sort(x=c(2.5,-1,-10,3.44),decreasing=FALSE)
## [1] -10.00  -1.00   2.50   3.44
myseq4 <- seq(from=5,to=-11,by=-0.3)
myseq4
##  [1]   5.0   4.7   4.4   4.1   3.8   3.5   3.2   2.9   2.6   2.3   2.0   1.7
## [13]   1.4   1.1   0.8   0.5   0.2  -0.1  -0.4  -0.7  -1.0  -1.3  -1.6  -1.9
## [25]  -2.2  -2.5  -2.8  -3.1  -3.4  -3.7  -4.0  -4.3  -4.6  -4.9  -5.2  -5.5
## [37]  -5.8  -6.1  -6.4  -6.7  -7.0  -7.3  -7.6  -7.9  -8.2  -8.5  -8.8  -9.1
## [49]  -9.4  -9.7 -10.0 -10.3 -10.6 -10.9
myseq5 <-rev(myseq4)
myseq5
##  [1] -10.9 -10.6 -10.3 -10.0  -9.7  -9.4  -9.1  -8.8  -8.5  -8.2  -7.9  -7.6
## [13]  -7.3  -7.0  -6.7  -6.4  -6.1  -5.8  -5.5  -5.2  -4.9  -4.6  -4.3  -4.0
## [25]  -3.7  -3.4  -3.1  -2.8  -2.5  -2.2  -1.9  -1.6  -1.3  -1.0  -0.7  -0.4
## [37]  -0.1   0.2   0.5   0.8   1.1   1.4   1.7   2.0   2.3   2.6   2.9   3.2
## [49]   3.5   3.8   4.1   4.4   4.7   5.0
a <- c(2,7,6)
b <- c(1,3,5)
a * b
## [1]  2 21 30
a %*% b
##      [,1]
## [1,]   53
vec7 = c(-1,3,-4,7,-9)
rep(vec7,times=2,each=10)
##   [1] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1  3  3  3  3  3  3  3  3  3  3 -4 -4 -4 -4 -4
##  [26] -4 -4 -4 -4 -4  7  7  7  7  7  7  7  7  7  7 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9
##  [51] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1  3  3  3  3  3  3  3  3  3  3 -4 -4 -4 -4 -4
##  [76] -4 -4 -4 -4 -4  7  7  7  7  7  7  7  7  7  7 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9
sort(vec7,decreasing=TRUE)
## [1]  7  3 -1 -4 -9
vec8i = c(from= 6,to= 12)
vec8ii <- c(5.3,times= 3)
vec8iii = c(-3)
vec8iv <- seq(from= 102, to=100, length= 9)
seq(from=3,to=6,length=5)
## [1] 3.00 3.75 4.50 5.25 6.00
vec0.2 = c(2,-5.1,-33,times=2)
7/42 +2
## [1] 2.166667
vec2.4 = c(seq,vec0.2,2.166667)
x = vec2.4 [c(1, length(vec0.2))]
y = vec2.4[-c(1,length(vec2.4))]
myvec = c(5,-2.3,4,4,4,6,8,10,40221,-8)
length(myvec)
## [1] 10
myvec[2]
## [1] -2.3
1:5
## [1] 1 2 3 4 5
foo <- 1:5
foo
## [1] 1 2 3 4 5
foo[-2]
## [1] 1 3 4 5
length(foo)
## [1] 5
foo * 4
## [1]  4  8 12 16 20
length(foo):2
## [1] 5 4 3 2
indexes <- c(4,rep(2,times=4),1,1,2,3:1)
indexes
##  [1] 4 2 2 2 2 1 1 2 3 2 1
foo[indexes]
##  [1] 4 2 2 2 2 1 1 2 3 2 1
foo
## [1] 1 2 3 4 5
foo[indexes]
##  [1] 4 2 2 2 2 1 1 2 3 2 1
foo[-c(1,3)]
## [1] 2 4 5
bar <- c(3,2,4,4,1,2,4,1,0,0,5)
bar
##  [1] 3 2 4 4 1 2 4 1 0 0 5
bar[1] <- 6
bar
##  [1] 6 2 4 4 1 2 4 1 0 0 5
bar[c(2,4,7)] <- c(-2,7,-8)
bar
##  [1]  6 -2  4  7  1  2 -8  1  0  0  5
seq(from=3,to=6,by=5)
## [1] 3
vec <- c(2,-5.1,-33)
vec
## [1]   2.0  -5.1 -33.0
rep(vec,times=2)
## [1]   2.0  -5.1 -33.0   2.0  -5.1 -33.0
rep(vec,times =2)
## [1]   2.0  -5.1 -33.0   2.0  -5.1 -33.0
 v <- 7/42 + 2
v
## [1] 2.166667
x <- seq(from=3,to=6,by=5)
vec11 <- c(2,-5.1,-33)
z <- 7/42 + 2
vec22 <- c(x,y,z)
vec22
## [[1]]
## [1] 3
## 
## [[2]]
## [1] 2
## 
## [[3]]
## [1] -5.1
## 
## [[4]]
## [1] -33
## 
## $times
## [1] 2
## 
## [[6]]
## [1] 2.166667
x
## [1] 3
y
## [[1]]
## [1] 2
## 
## [[2]]
## [1] -5.1
## 
## [[3]]
## [1] -33
## 
## $times
## [1] 2
rep(vec11,times=2)
## [1]   2.0  -5.1 -33.0   2.0  -5.1 -33.0
y1 <- (rep(vec11,times=2))
vec22 <- c(x,y1,z)
vec22
## [1]   3.000000   2.000000  -5.100000 -33.000000   2.000000  -5.100000 -33.000000
## [8]   2.166667
vec22[1]
## [1] 3
vec22[length(vec22)]
## [1] 2.166667
boy <- vec22[1]
boy2 <- vec22[length(vec22)]
vec22[-c(1, length(vec22))]
## [1]   2.0  -5.1 -33.0   2.0  -5.1 -33.0
boy3 <- vec22[-c(1, length(vec22))]
girl <- c(boy,boy2,boy3)
girl
## [1]   3.000000   2.166667   2.000000  -5.100000 -33.000000   2.000000  -5.100000
## [8] -33.000000
vec22 <- sort(vec22)
vec22
## [1] -33.000000 -33.000000  -5.100000  -5.100000   2.000000   2.000000   2.166667
## [8]   3.000000
rev(vec22)
## [1]   3.000000   2.166667   2.000000   2.000000  -5.100000  -5.100000 -33.000000
## [8] -33.000000
boy3
## [1]   2.0  -5.1 -33.0   2.0  -5.1 -33.0
boy3[3]
## [1] -33
boy3[6]
## [1] -33
rep(boy3[3],times=3)
## [1] -33 -33 -33
rep(boy3[6],times=4)
## [1] -33 -33 -33 -33
c1 <- (rep(boy3[3],times=3))
c2 <- (rep(boy3[6],times=4))
vec3 <- c(c1,c2,-33)
vec3
## [1] -33 -33 -33 -33 -33 -33 -33 -33
matrix(c(1,2,3,4,5,6),nrow=2,ncol=3,byrow=TRUE)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
rbind(1:3,4:6)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
cbind(c(1,4),c(2,5),c(3,6))
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
mymat <- rbind(c(1,2,3),5:3,c(100,20,90),11:13)
mymat
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    5    4    3
## [3,]  100   20   90
## [4,]   11   12   13
 dim(mymat)[2]
## [1] 3
 dim(mymat)[3]
## [1] NA
 matrix(c(0.3,4.5,55.3,91,0.1,105.5,-4.2,8.2,27.9),nrow=3,ncol=3)
##      [,1]  [,2] [,3]
## [1,]  0.3  91.0 -4.2
## [2,]  4.5   0.1  8.2
## [3,] 55.3 105.5 27.9
A <-  matrix(c(0.3,4.5,55.3,91,0.1,105.5,-4.2,8.2,27.9),nrow=3,ncol=3)
 A[3,2]
## [1] 105.5
A[1,3]
## [1] -4.2
 A[2:3,]
##      [,1]  [,2] [,3]
## [1,]  4.5   0.1  8.2
## [2,] 55.3 105.5 27.9
A[-1,]
##      [,1]  [,2] [,3]
## [1,]  4.5   0.1  8.2
## [2,] 55.3 105.5 27.9
 A[-2,-c(2,3)]
## [1]  0.3 55.3
b <- A
b
##      [,1]  [,2] [,3]
## [1,]  0.3  91.0 -4.2
## [2,]  4.5   0.1  8.2
## [3,] 55.3 105.5 27.9
b[2,] <- 1:3
b[2,]
## [1] 1 2 3
b
##      [,1]  [,2] [,3]
## [1,]  0.3  91.0 -4.2
## [2,]  1.0   2.0  3.0
## [3,] 55.3 105.5 27.9
 b[c(1,3),2] <- 900
b
##      [,1] [,2] [,3]
## [1,]  0.3  900 -4.2
## [2,]  1.0    2  3.0
## [3,] 55.3  900 27.9
B <- matrix(c(4.3,3.1,8.2,8.2,3.2,0.9,1.6,6.5),nrow=4,ncol=,byrow=TRUE)
B
##      [,1] [,2]
## [1,]  4.3  3.1
## [2,]  8.2  8.2
## [3,]  3.2  0.9
## [4,]  1.6  6.5
B[-4,]
##      [,1] [,2]
## [1,]  4.3  3.1
## [2,]  8.2  8.2
## [3,]  3.2  0.9
dim(B[-4,])
## [1] 3 2
sort(B[,2])
## [1] 0.9 3.1 6.5 8.2
B2c <- sort(B[,2])
B[,2] <- B2c
B2c
## [1] 0.9 3.1 6.5 8.2
a <- diag(x=3)
a
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1
A <- rbind(c(2,5,2),c(6,1,4))
a <- 2
a*A
##      [,1] [,2] [,3]
## [1,]    4   10    4
## [2,]   12    2    8
B <- cbind(c(3,-1,1),c(-3,1,5))
A%*%B
##      [,1] [,2]
## [1,]    3    9
## [2,]   21    3
A <- cbind(c(1,2,7),c(2,4,6))
A
##      [,1] [,2]
## [1,]    1    2
## [2,]    2    4
## [3,]    7    6
B <- cbind(c(10,30,50),c(20,40,60))
B
##      [,1] [,2]
## [1,]   10   20
## [2,]   30   40
## [3,]   50   60
a <- 2/7
a*(A-B)
##            [,1]       [,2]
## [1,]  -2.571429  -5.142857
## [2,]  -8.000000 -10.285714
## [3,] -12.285714 -15.428571
D <- cbind(c(1,2,7))
D
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    7
E <- cbind(c(3,4,8))
E
##      [,1]
## [1,]    3
## [2,]    4
## [3,]    8
dim(D)
## [1] 3 1
dim(E)
## [1] 3 1
t(D)
##      [,1] [,2] [,3]
## [1,]    1    2    7
t(D)%*%E
##      [,1]
## [1,]   67
t(E)
##      [,1] [,2] [,3]
## [1,]    3    4    8
D%*%t(D)
##      [,1] [,2] [,3]
## [1,]    1    2    7
## [2,]    2    4   14
## [3,]    7   14   49
t(E)%*%(D%*%t(D))
##      [,1] [,2] [,3]
## [1,]   67  134  469
(E%*%t(E))+(D%*%t(D))-100*diag(3)
##      [,1] [,2] [,3]
## [1,]  -90   14   31
## [2,]   14  -80   46
## [3,]   31   46   13
A <- rbind(c(2,0,0,0),c(0,3,0,0),c(0,0,5,0),c(0,0,0,-1),nrow=4,ncol=4)
A
##      [,1] [,2] [,3] [,4]
##         2    0    0    0
##         0    3    0    0
##         0    0    5    0
##         0    0    0   -1
## nrow    4    4    4    4
## ncol    4    4    4    4
AR <- array(data=1:24,dim=c(3,4,2))
AR
## , , 1
## 
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
## 
## , , 2
## 
##      [,1] [,2] [,3] [,4]
## [1,]   13   16   19   22
## [2,]   14   17   20   23
## [3,]   15   18   21   24
BR <- array(data=rep(1:24,times=3),dim=c(3,4,2,3))
BR
## , , 1, 1
## 
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
## 
## , , 2, 1
## 
##      [,1] [,2] [,3] [,4]
## [1,]   13   16   19   22
## [2,]   14   17   20   23
## [3,]   15   18   21   24
## 
## , , 1, 2
## 
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
## 
## , , 2, 2
## 
##      [,1] [,2] [,3] [,4]
## [1,]   13   16   19   22
## [2,]   14   17   20   23
## [3,]   15   18   21   24
## 
## , , 1, 3
## 
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
## 
## , , 2, 3
## 
##      [,1] [,2] [,3] [,4]
## [1,]   13   16   19   22
## [2,]   14   17   20   23
## [3,]   15   18   21   24
AR <- array(data=rep(1:24,times=3),dim=c(3,4,2,3))
AR
## , , 1, 1
## 
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
## 
## , , 2, 1
## 
##      [,1] [,2] [,3] [,4]
## [1,]   13   16   19   22
## [2,]   14   17   20   23
## [3,]   15   18   21   24
## 
## , , 1, 2
## 
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
## 
## , , 2, 2
## 
##      [,1] [,2] [,3] [,4]
## [1,]   13   16   19   22
## [2,]   14   17   20   23
## [3,]   15   18   21   24
## 
## , , 1, 3
## 
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
## 
## , , 2, 3
## 
##      [,1] [,2] [,3] [,4]
## [1,]   13   16   19   22
## [2,]   14   17   20   23
## [3,]   15   18   21   24
AR[2,,,2]
##      [,1] [,2]
## [1,]    2   14
## [2,]    5   17
## [3,]    8   20
## [4,]   11   23
AZ <- array(seq(from=4.8,to=0.1,length=48,),c(dim=4,2,6))
AZ
## , , 1
## 
##      [,1] [,2]
## [1,]  4.8  4.4
## [2,]  4.7  4.3
## [3,]  4.6  4.2
## [4,]  4.5  4.1
## 
## , , 2
## 
##      [,1] [,2]
## [1,]  4.0  3.6
## [2,]  3.9  3.5
## [3,]  3.8  3.4
## [4,]  3.7  3.3
## 
## , , 3
## 
##      [,1] [,2]
## [1,]  3.2  2.8
## [2,]  3.1  2.7
## [3,]  3.0  2.6
## [4,]  2.9  2.5
## 
## , , 4
## 
##      [,1] [,2]
## [1,]  2.4  2.0
## [2,]  2.3  1.9
## [3,]  2.2  1.8
## [4,]  2.1  1.7
## 
## , , 5
## 
##      [,1] [,2]
## [1,]  1.6  1.2
## [2,]  1.5  1.1
## [3,]  1.4  1.0
## [4,]  1.3  0.9
## 
## , , 6
## 
##      [,1] [,2]
## [1,]  0.8  0.4
## [2,]  0.7  0.3
## [3,]  0.6  0.2
## [4,]  0.5  0.1
B <- AZ[c(4,1), 2, ]
B
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]  4.1  3.3  2.5  1.7  0.9  0.1
## [2,]  4.4  3.6  2.8  2.0  1.2  0.4
C <- array(rep(B[2, ], 4), dim = c(2, 2, 2, 3))
C
## , , 1, 1
## 
##      [,1] [,2]
## [1,]  4.4  2.8
## [2,]  3.6  2.0
## 
## , , 2, 1
## 
##      [,1] [,2]
## [1,]  1.2  4.4
## [2,]  0.4  3.6
## 
## , , 1, 2
## 
##      [,1] [,2]
## [1,]  2.8  1.2
## [2,]  2.0  0.4
## 
## , , 2, 2
## 
##      [,1] [,2]
## [1,]  4.4  2.8
## [2,]  3.6  2.0
## 
## , , 1, 3
## 
##      [,1] [,2]
## [1,]  1.2  4.4
## [2,]  0.4  3.6
## 
## , , 2, 3
## 
##      [,1] [,2]
## [1,]  2.8  1.2
## [2,]  2.0  0.4
D <- AZ[,,-6]
D
## , , 1
## 
##      [,1] [,2]
## [1,]  4.8  4.4
## [2,]  4.7  4.3
## [3,]  4.6  4.2
## [4,]  4.5  4.1
## 
## , , 2
## 
##      [,1] [,2]
## [1,]  4.0  3.6
## [2,]  3.9  3.5
## [3,]  3.8  3.4
## [4,]  3.7  3.3
## 
## , , 3
## 
##      [,1] [,2]
## [1,]  3.2  2.8
## [2,]  3.1  2.7
## [3,]  3.0  2.6
## [4,]  2.9  2.5
## 
## , , 4
## 
##      [,1] [,2]
## [1,]  2.4  2.0
## [2,]  2.3  1.9
## [3,]  2.2  1.8
## [4,]  2.1  1.7
## 
## , , 5
## 
##      [,1] [,2]
## [1,]  1.6  1.2
## [2,]  1.5  1.1
## [3,]  1.4  1.0
## [4,]  1.3  0.9
D[c(2,4), 2, c(1,3,5)] <- -99
D
## , , 1
## 
##      [,1]  [,2]
## [1,]  4.8   4.4
## [2,]  4.7 -99.0
## [3,]  4.6   4.2
## [4,]  4.5 -99.0
## 
## , , 2
## 
##      [,1] [,2]
## [1,]  4.0  3.6
## [2,]  3.9  3.5
## [3,]  3.8  3.4
## [4,]  3.7  3.3
## 
## , , 3
## 
##      [,1]  [,2]
## [1,]  3.2   2.8
## [2,]  3.1 -99.0
## [3,]  3.0   2.6
## [4,]  2.9 -99.0
## 
## , , 4
## 
##      [,1] [,2]
## [1,]  2.4  2.0
## [2,]  2.3  1.9
## [3,]  2.2  1.8
## [4,]  2.1  1.7
## 
## , , 5
## 
##      [,1]  [,2]
## [1,]  1.6   1.2
## [2,]  1.5 -99.0
## [3,]  1.4   1.0
## [4,]  1.3 -99.0
 baz <- c(T,F,F,F,T,F,T,T,T,F,T,F)
baz
##  [1]  TRUE FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE FALSE
length(baz)
## [1] 12
 tee <- matrix(data=baz,nrow=3,ncol=4,byrow=TRUE)
tee
##      [,1]  [,2]  [,3]  [,4]
## [1,] TRUE FALSE FALSE FALSE
## [2,] TRUE FALSE  TRUE  TRUE
## [3,] TRUE FALSE  TRUE FALSE
1==2
## [1] FALSE
 foo <- c(3,2,1,4,1,2,1,-1,0,3)
 bar <- c(4,1,2,1,1,0,0,3,0,4)
 length(x=foo)==length(x=bar)
## [1] TRUE
foo==bar
##  [1] FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE
foo<bar
##  [1]  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE  TRUE
foo<=(bar+10)
##  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
 c(foo,nrow=5,ncol=2)
##                                                   nrow ncol 
##    3    2    1    4    1    2    1   -1    0    3    5    2
toz =  c(6,9,7,3,6,7,9,6,3,6,6,7,1,9,1)
toz==6
##  [1]  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE
## [13] FALSE FALSE FALSE
toz>=6
##  [1]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
## [13] FALSE  TRUE FALSE
toz<(6+2)
##  [1]  TRUE FALSE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
## [13]  TRUE FALSE  TRUE
toz!=2
##  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
toz1 <- toz[-c(1:3)]
T <- array(toz1,dim=c(2,2,3))
T
## , , 1
## 
##      [,1] [,2]
## [1,]    3    7
## [2,]    6    9
## 
## , , 2
## 
##      [,1] [,2]
## [1,]    6    6
## [2,]    3    6
## 
## , , 3
## 
##      [,1] [,2]
## [1,]    7    9
## [2,]    1    1
T<=(6/2+4)
## , , 1
## 
##      [,1]  [,2]
## [1,] TRUE  TRUE
## [2,] TRUE FALSE
## 
## , , 2
## 
##      [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE
## 
## , , 3
## 
##      [,1]  [,2]
## [1,] TRUE FALSE
## [2,] TRUE  TRUE
T>=6
## , , 1
## 
##       [,1] [,2]
## [1,] FALSE TRUE
## [2,]  TRUE TRUE
## 
## , , 2
## 
##       [,1] [,2]
## [1,]  TRUE TRUE
## [2,] FALSE TRUE
## 
## , , 3
## 
##       [,1]  [,2]
## [1,]  TRUE  TRUE
## [2,] FALSE FALSE
T<6+2
## , , 1
## 
##      [,1]  [,2]
## [1,] TRUE  TRUE
## [2,] TRUE FALSE
## 
## , , 2
## 
##      [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE
## 
## , , 3
## 
##      [,1]  [,2]
## [1,] TRUE FALSE
## [2,] TRUE  TRUE
T!=6
## , , 1
## 
##       [,1] [,2]
## [1,]  TRUE TRUE
## [2,] FALSE TRUE
## 
## , , 2
## 
##       [,1]  [,2]
## [1,] FALSE FALSE
## [2,]  TRUE FALSE
## 
## , , 3
## 
##      [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE TRUE
S <- diag(10)
S
##       [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
##  [1,]    1    0    0    0    0    0    0    0    0     0
##  [2,]    0    1    0    0    0    0    0    0    0     0
##  [3,]    0    0    1    0    0    0    0    0    0     0
##  [4,]    0    0    0    1    0    0    0    0    0     0
##  [5,]    0    0    0    0    1    0    0    0    0     0
##  [6,]    0    0    0    0    0    1    0    0    0     0
##  [7,]    0    0    0    0    0    0    1    0    0     0
##  [8,]    0    0    0    0    0    0    0    1    0     0
##  [9,]    0    0    0    0    0    0    0    0    1     0
## [10,]    0    0    0    0    0    0    0    0    0     1
S==0
##        [,1]  [,2]  [,3]  [,4]  [,5]  [,6]  [,7]  [,8]  [,9] [,10]
##  [1,] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [2,]  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [3,]  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [4,]  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [5,]  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
##  [6,]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE
##  [7,]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE
##  [8,]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE
##  [9,]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE
## [10,]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
logicalarray <- c(TRUE,FALSE,TRUE)
#check if any values are TRUE 
anytrue <- any(logicalarray) # returns TRUE if atleast one element is TRUE
# check if all values are TRUE 
alltrue <- all(logicalarray) # returns TRUE only if every element is TRUE
anytrue
## [1] TRUE
alltrue
## [1] FALSE
# suppose you have a logical array
logicalarray = any(logicalarray) # returns TRUE if at least one element is TRUE  
# suppose you have a logical array
logicalarray <- c(TRUE,FALSE,TRUE)
# check if all values are TRUE
alltrue <- all(logicalarray) # returns TRUE if every element is TRUE
anytrue
## [1] TRUE
alltrue
## [1] FALSE
#suppose you have a logical matrix erlier 
logicalmatrix <- (toz==6)
logicalmatrix
##  [1]  TRUE FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE
## [13] FALSE FALSE FALSE
# extract the diagonal element 
diagelement <- diag(logicalmatrix)
# check if any diagonal entries are TRUE
 any(diagelement) 
## [1] TRUE
 foo <- c(T,F,F,F,T,F,T,T,T,F,T,F)
 bar <- c(F,T,F,T,F,F,F,F,T,T,T,T)
 foo&bar
##  [1] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [13] FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [25]  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [37]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [49]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [61]  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [73]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
rex <- c(7,1,7,10,5,9,10,3,10,8)
rex > 5|2
##  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
bax <- c(8,8,4,4,5,1,5,6,6,8)
bax33 <- bar[(bar <= 6)& (bar!= 4)]
bax33
##  [1] 0 3 6 6 3 6 6 1 1 0 3 6 6 3 6 6 1 1 0 0 0 0 3 6 6 3 6 6 1 1 3 6 6 3 6 6 1 1
## [39] 3 6 6 3 6 6 1 1 3 6 6 3 6 6 1 1
rex&bax33
## Warning in rex & bax33: longer object length is not a multiple of shorter
## object length
##  [1] FALSE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE
## [13]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE  TRUE  TRUE
## [25]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [37]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
## [49]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE
T <- rex + bax
T 
##  [1] 15  9 11 14 10 10 15  9 16 16
TX <- T[(T>= 14)& (T!= 15)]
TX
## [1] 14 16 16
TRUE+TRUE
## [1] 2
 myvec <- c(5,-2.3,4,4,4,6,8,10,40221,-8)
 myvec[c(F,T,F,F,F,F,F,F,F,T)]
##  [1]    NA 40221    NA    NA    -8    -8    NA 40221    NA    NA    NA 40221
## [13]    NA    NA    -8    -8    NA 40221    NA    NA
 myvec<0
##  [1] FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE  TRUE
 myvec[myvec<0]
## [1] -2.3 -8.0
 myvec[c(T,F)]
##  [1]    NA 40221    NA    NA    -8    -8    NA 40221    NA    NA
 myvec[(myvec>0)&(myvec<1000)]
## [1]  5  4  4  4  6  8 10
 myvec[myvec<0] <- -200
myvec
##  [1]     5  -200     4     4     4     6     8    10 40221  -200
 myvec[-which(x=myvec<0)]
## [1]     5     4     4     4     6     8    10 40221
foo <- c(7,5,6,1,2,10,8,3,8,2)
 foo[foo>=5]
## [1]  7  5  6 10  8  8
bar <- foo[foo>=5]
tox <- foo[!(foo>=5)]
tox
## [1] 1 2 3 2
matrix(bar,nrow=2,ncol=3,byrow=TRUE)
##      [,1] [,2] [,3]
## [1,]    7    5    6
## [2,]   10    8    8
baz <- matrix(bar,nrow=2,ncol=3,byrow=TRUE)
baz[baz==8] <- (baz[1,2])^2
baz
##      [,1] [,2] [,3]
## [1,]    7    5    6
## [2,]   10   25   25
all(baz<=25&baz>4)
## [1] TRUE
vec2 <- c(10,5,1,4,7,4,3,3,1,3,4,3,1,7,8,3,7,3)
qux <- array(vec2,dim=c(3,2,3))
qux
## , , 1
## 
##      [,1] [,2]
## [1,]   10    4
## [2,]    5    7
## [3,]    1    4
## 
## , , 2
## 
##      [,1] [,2]
## [1,]    3    3
## [2,]    3    4
## [3,]    1    3
## 
## , , 3
## 
##      [,1] [,2]
## [1,]    1    3
## [2,]    7    7
## [3,]    8    3
indices <- which(qux==3|qux==4,arr.ind=TRUE)
indices
##       dim1 dim2 dim3
##  [1,]    1    2    1
##  [2,]    3    2    1
##  [3,]    1    1    2
##  [4,]    2    1    2
##  [5,]    1    2    2
##  [6,]    2    2    2
##  [7,]    3    2    2
##  [8,]    1    2    3
##  [9,]    3    2    3
qux[qux<3|qux>=7] <- 100
qux
## , , 1
## 
##      [,1] [,2]
## [1,]  100    4
## [2,]    5  100
## [3,]  100    4
## 
## , , 2
## 
##      [,1] [,2]
## [1,]    3    3
## [2,]    3    4
## [3,]  100    3
## 
## , , 3
## 
##      [,1] [,2]
## [1,]  100    3
## [2,]  100  100
## [3,]  100    3
foo[c(FALSE,TRUE)]
## [1]  5  1 10  3  2
foo[c(0,1)]
## [1] 7
"alpha"=="alpha"
## [1] TRUE
"alpha"!="beta"
## [1] TRUE
c("alpha","beta","gamma")=="beta"
## [1] FALSE  TRUE FALSE
 qux <- c("awesome","R","is")
 length(qux)
## [1] 3
qux
## [1] "awesome" "R"       "is"
cat(qux[2],qux[3],"totally",qux[1],"!")
## R is totally awesome !
paste(qux[2],qux[3],"totally",qux[1],"!")
## [1] "R is totally awesome !"
paste(qux[3],qux[2],"totally",qux[1],"?")
## [1] "is R totally awesome ?"
a <- 3
b <- 4.4 
cat("the value stored as 'a' is ",a,".",sep="")
## the value stored as 'a' is 3.
paste("the value stored as 'b' is ",b,".",sep="")
## [1] "the value stored as 'b' is 4.4."
cat("the result of a+b is ",a,"+",b,"=",a+b,".",sep="")
## the result of a+b is 3+4.4=7.4.
paste("is",a+b,"less than 10 ?that's totally ",a+b<10,".",sep="")
## [1] "is7.4less than 10 ?that's totally TRUE."
cat("here is a string\nsplit\tto new\b\n\n\tlines")
## here is a string
## split    to new
## 
##  lines
cat("i really want a backslash: \\\nand a double quote: \"")
## i really want a backslash: \
## and a double quote: "
foo <- "this is a character string!"
substr(foo,start=21,stop=27)
## [1] "string!"
substr(foo,start=1,stop=4) <- "here"
foo
## [1] "here is a character string!"
bar <- "how much could a woodchuck chuck"
sub(pattern="chuck",replacement="hurl",bar)
## [1] "how much could a woodhurl chuck"
gsub(pattern="chuck",replacement="hurl",bar)
## [1] "how much could a woodhurl hurl"
cat("the quick brown fox\n jumped over\t\n\tthe lazy dogs")
## the quick brown fox
##  jumped over 
##  the lazy dogs
num1 <- 4
num2 <- 0.75
cat("the value stored as 'num1' is ",num1,".",sep="")
## the value stored as 'num1' is 4.
paste("the value stored as 'num2' is ",num2,".",sep="")
## [1] "the value stored as 'num2' is 0.75."
cat("the result of num1*num2 is ",num1,"*",num2,"=",num1*num2,".",sep="")
## the result of num1*num2 is 4*0.75=3.
A <- ("/users/tdavies/document/rbook/")
bar
## [1] "how much could a woodchuck chuck"
bar <- ("if a woodchuck could chuck wood")
bar
## [1] "if a woodchuck could chuck wood"
gsub(pattern="wood",replacement="metal",bar)
## [1] "if a metalchuck could chuck metal"
s <- "Two 6-packs for $12.99"
substr(s,5,10)  == "6-pack"
## [1] TRUE
gsub(pattern="\\$12\\.99",replacement="\\$10.99",x=s)
## [1] "Two 6-packs for $10.99"
s
## [1] "Two 6-packs for $12.99"
# examles under factor
firstname <- c("liz","jolene","susan","boris","rochelle","tim","simon","amy")
sex.num <- c(0,0,0,1,0,1,1,0)
sex.num.fac <- factor(sex.num)
sex.num.fac
## [1] 0 0 0 1 0 1 1 0
## Levels: 0 1
levels(sex.num.fac)
## [1] "0" "1"
#relabelling the sex to 1 & 2 from 0 & 1
levels(sex.num.fac) <- c("1","2")
sex.num.fac[2:5]
## [1] 1 1 2 1
## Levels: 1 2
# test for identifying all men
sex.num.fac=="2"
## [1] FALSE FALSE FALSE  TRUE FALSE  TRUE  TRUE FALSE
# defining and ordering levels
mob <- c("apr","jan","dec","sep","nov","jul","jul","jun")
# ccomparing jan and dec
mob[2]<mob[3]
## [1] FALSE
ms <- c("jan","feb","mar","apr","may","jun","jul","aug","sep","oct","nov","dec")
mob.fac <- factor(mob,levels=ms,ordered=TRUE)
mob.fac
## [1] apr jan dec sep nov jul jul jun
## 12 Levels: jan < feb < mar < apr < may < jun < jul < aug < sep < ... < dec
#cutting and combining
foo <- c(5.1,3.3,3.1,4)
bar <- c(4.5,1.2)
c(foo,bar)
## [1] 5.1 3.3 3.1 4.0 4.5 1.2
new.values <- factor(c("oct","feb","feb"),levels=levels(mob.fac),ordered=TRUE)
new.values
## [1] oct feb feb
## 12 Levels: jan < feb < mar < apr < may < jun < jul < aug < sep < ... < dec
 levels(mob.fac)
##  [1] "jan" "feb" "mar" "apr" "may" "jun" "jul" "aug" "sep" "oct" "nov" "dec"
 levels(mob.fac)[c(mob.fac,new.values)]
##  [1] "apr" "jan" "dec" "sep" "nov" "jul" "jul" "jun" "oct" "feb" "feb"
 # turning the vectors into factor object
 mob.new <- levels(mob.fac)[c(mob.fac,new.values)]
 mob.new.fac <- factor(mob.new,levels=levels(mob.fac),ordered=TRUE)
 mob.new.fac
##  [1] apr jan dec sep nov jul jul jun oct feb feb
## 12 Levels: jan < feb < mar < apr < may < jun < jul < aug < sep < ... < dec
# last example before exercise
y <-  c(0.53,5.4,1.5,3.33,0.45,0.01,2,4.2,1.99,1.01)
br <- c(0,2,4,6)
cut(y,breaks=br)
##  [1] (0,2] (4,6] (0,2] (2,4] (0,2] (0,2] (0,2] (4,6] (0,2] (0,2]
## Levels: (0,2] (2,4] (4,6]
cut(y,breaks=br,right=FALSE)
##  [1] [0,2) [4,6) [0,2) [2,4) [0,2) [0,2) [2,4) [4,6) [0,2) [0,2)
## Levels: [0,2) [2,4) [4,6)
lab <- c("small","medium","large")
cut(y,breaks=br,right=FALSE,include.lowest=TRUE,labels=lab)
##  [1] small  large  small  medium small  small  medium large  small  small 
## Levels: small medium large
#exercise 4.5
#(a) create the character vector
sex <- rep("M",20)
sex[c(1,5,6,7,12,14,15,16)] <- "F"
# party vector
party <- rep("national",20)
party[c(1,4,12,15,16,19)] <- "labour"
party[c(6,9,11)] <- "greens"
party[c(10,20)] <- "others"
#(b) 
sex.fac <- factor(sex)
party.fac <- factor(party)
# checking for levels
levels(sex.fac)
## [1] "F" "M"
levels(party.fac)
## [1] "greens"   "labour"   "national" "others"
#(c)
party.fac[sex.fac=="M"]
##  [1] national national labour   national greens   others   greens   national
##  [9] national national labour   others  
## Levels: greens labour national others
sex.fac[party.fac=="national"]
## [1] M M F F M M F M M
## Levels: F M
# (d)
new.party <- factor(c("national","maori","maori","labour","greens","labour"),levels=union(levels(party.fac),c("national","labour","greens","maori","other")))
new.sex <- factor(c("M","M","F","F","M"))
# combine
party.all <- c(party.fac,new.party)
sex.all <- c(sex.fac,new.sex)
party.all
##  [1] labour   national national labour   national greens   national national
##  [9] greens   others   greens   labour   national national labour   labour  
## [17] national national labour   others   national maori    maori    labour  
## [25] greens   labour  
## Levels: greens labour national others maori other
sex.all
##  [1] F M M M F F F M M M M F M F F F M M M M M M F F M
## Levels: F M
#(e)
confidence <- c( 93, 55, 29, 100,
52, 84, 56, 0, 33, 52, 35, 53, 55, 46, 40, 40, 56, 45, 64, 31, 10, 29, 40,
95, 18, 61.)
conf.level <- cut(confidence,breaks=c(0,30,70,100),labels=c("low","moderate","high"),include.lowest=TRUE)
conf.level
##  [1] high     moderate low      high     moderate high     moderate low     
##  [9] moderate moderate moderate moderate moderate moderate moderate moderate
## [17] moderate moderate moderate moderate low      low      moderate high    
## [25] low      moderate
## Levels: low moderate high
# (f)
# original 20 participants only
orig.party <- party.fac
labour.conf <- conf.level[orig.party=="labour"]
labour.conf
## [1] high     high     moderate moderate moderate moderate low      high    
## Levels: low moderate high
national.conf <- conf.level[orig.party=="national"]
national.conf
##  [1] moderate low      moderate moderate low      moderate moderate moderate
##  [9] moderate low      moderate low     
## Levels: low moderate high
table(labour.conf)
## labour.conf
##      low moderate     high 
##        1        4        3
table(national.conf)
## national.conf
##      low moderate     high 
##        4        8        0
foo <- list(matrix(data=1:4,nrow=2,ncol=2),c(TRUE,FALSE,TRUE,TRUE),"hello")
foo
## [[1]]
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
## 
## [[2]]
## [1]  TRUE FALSE  TRUE  TRUE
## 
## [[3]]
## [1] "hello"
foo[[1]]
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
 foo[[1]][2,1]
## [1] 2
  cat(foo[[3]],"you!")
## hello you!
  bar <- foo[c(2,3)]
  bar
## [[1]]
## [1]  TRUE FALSE  TRUE  TRUE
## 
## [[2]]
## [1] "hello"
  # naming
  names(foo) <- c("mymatrix","mylogical","mystring")
  foo
## $mymatrix
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
## 
## $mylogical
## [1]  TRUE FALSE  TRUE  TRUE
## 
## $mystring
## [1] "hello"
   baz <- list(tom=c(foo[[2]],TRUE,TRUE,TRUE,FALSE),dick="g'day mate",harry=foo$mymatrix*2)
  baz
## $tom
## [1]  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
## 
## $dick
## [1] "g'day mate"
## 
## $harry
##      [,1] [,2]
## [1,]    2    6
## [2,]    4    8
  names(baz)
## [1] "tom"   "dick"  "harry"
  # nesting
  baz$bobby <- foo
  baz
## $tom
## [1]  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE
## 
## $dick
## [1] "g'day mate"
## 
## $harry
##      [,1] [,2]
## [1,]    2    6
## [2,]    4    8
## 
## $bobby
## $bobby$mymatrix
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
## 
## $bobby$mylogical
## [1]  TRUE FALSE  TRUE  TRUE
## 
## $bobby$mystring
## [1] "hello"
  baz$bobby$mylogicals[1:3]
## NULL
   baz[[4]][[2]][1:3]
## [1]  TRUE FALSE  TRUE
   baz[[4]]$mylogicals[1:3]
## NULL
 foo <- c("character","a",NA,"with","string",NA)
foo
## [1] "character" "a"         NA          "with"      "string"    NA
 bar <- factor(c("blue",NA,NA,"blue","green","blue",NA,"red","red",NA,
"green"))
 bar
##  [1] blue  <NA>  <NA>  blue  green blue  <NA>  red   red   <NA>  green
## Levels: blue green red
  baz <- matrix(c(1:3,NA,5,6,NA,8,NA),nrow=3,ncol=3)
baz
##      [,1] [,2] [,3]
## [1,]    1   NA   NA
## [2,]    2    5    8
## [3,]    3    6   NA
 qux <- c(NA,5.89,Inf,NA,9.43,-2.35,NaN,2.10,-8.53,-7.58,NA,-4.58,2.01,NaN)
 qux
##  [1]    NA  5.89   Inf    NA  9.43 -2.35   NaN  2.10 -8.53 -7.58    NA -4.58
## [13]  2.01   NaN
 is.na(qux)
##  [1]  TRUE FALSE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE FALSE  TRUE FALSE
## [13] FALSE  TRUE
  which(x=is.nan(x=qux))
## [1]  7 14
   which(x=(is.na(x=qux)&!is.nan(x=qux)))
## [1]  1  4 11
    quux <- na.omit(object=qux)
    quux
## [1]  5.89   Inf  9.43 -2.35  2.10 -8.53 -7.58 -4.58  2.01
## attr(,"na.action")
## [1]  1  4  7 11 14
## attr(,"class")
## [1] "omit"
    # NULL
     foo <- NULL
     foo
## NULL
     bar <- NA
     bar
## [1] NA
      c(2,4,NA,8)
## [1]  2  4 NA  8
       c(2,4,NULL,8)
## [1] 2 4 8
        c(NA,NA,NA)
## [1] NA NA NA
         c(NULL,NULL,NULL)
## NULL
          opt.arg <- c("string1","string2","string3")
          is.na(x=opt.arg)
## [1] FALSE FALSE FALSE
           is.null(x=opt.arg)
## [1] FALSE
            opt.arg <- c(NA,NA,NA)
            is.na(x=opt.arg)
## [1] TRUE TRUE TRUE
             opt.arg <- c(NULL,NULL,NULL)
             is.null(x=opt.arg)
## [1] TRUE
              foo <- list(member1=c(33,1,5.2,7),member2="NA or NULL?")
              foo
## $member1
## [1] 33.0  1.0  5.2  7.0
## 
## $member2
## [1] "NA or NULL?"
               foo$member3
## NULL
                foo$member3 <- NA
                 foo
## $member1
## [1] 33.0  1.0  5.2  7.0
## 
## $member2
## [1] "NA or NULL?"
## 
## $member3
## [1] NA
                 # exercise 6.2
# basic plotting 
#7.1 Using plot with Coordinate Vectors
 foo <- c(1.1,2,3.5,3.9,4.2)
 bar <- c(2,2.2,-1.3,0,0.2)
  plot(foo,bar)

   baz <- cbind(foo,bar)
   baz
##      foo  bar
## [1,] 1.1  2.0
## [2,] 2.0  2.2
## [3,] 3.5 -1.3
## [4,] 3.9  0.0
## [5,] 4.2  0.2
   #7.2.1 Automatic Plot Types
    plot(foo,bar,type="l")

    #7.2.2 Title and Axis Labels
     plot(foo,bar,type="b",main="My lovely plot\ntitle on two lines",xlab="",
ylab="")

      plot(foo,bar,type="b",main="My lovely plot",xlab="",ylab="",col=2)

       plot(foo,bar,type="b",main="My lovely plot",xlab="",ylab="",col="seagreen4")

       #Line and Point Appearances 7.2.4
        plot(foo,bar,type="b",main="My lovely plot",xlab="",ylab="",
col=4,pch=8,lty=2,cex=2.3,lwd=3.3)

         plot(foo,bar,type="b",main="My lovely plot",xlab="",ylab="",
col=6,pch=15,lty=3,cex=0.7,lwd=2)

         x <- 1:20
          y <- c(-1.49,3.37,2.59,-2.78,-3.94,-0.92,6.43,8.51,3.41,-8.23,-12.01,-6.58,2.87,14.12,9.63,-4.58,-14.78,-11.67,1.17,15.62)
           plot(x,y,type="n",main="")
             segments(x0=c(5,15),y0=c(-5,-5),x1=c(5,15),y1=c(5,5),col="red",lty=3,
lwd=2)
              points(x[y>=5],y[y>=5],pch=4,col="darkmagenta",cex=2)
               points(x[y<=-5],y[y<=-5],pch=3,col="darkgreen",cex=2)
                points(x[(x>=5&x<=15)&(y>-5&y<5)],y[(x>=5&x<=15)&(y>-5&y<5)],pch=19,
col="blue")
                 points(x[(x<5|x>15)&(y>-5&y<5)],y[(x<5|x>15)&(y>-5&y<5)])
       lines(x,y,lty=4)
        arrows(x0=8,y0=14,x1=11,y1=2.5)
         text(x=8,y=15,labels="sweet spot")
         legend("bottomleft",
legend=c("overall process","sweet","standard",
"too big","too small","sweet y range","sweet x range"),
pch=c(NA,19,1,4,3,NA,NA),lty=c(4,NA,NA,NA,NA,2,3),
col=c("black","blue","black","darkmagenta","darkgreen","red","red"),
lwd=c(1,NA,NA,NA,NA,2,2),pt.cex=c(NA,1,1,2,2,NA,NA))

         #exercise 7.1
         # create an empty plotting area
         plot(xlim<- c(-3,3),ylim <- c(7,13),xlab="",ylab="",axes=FALSE)
         # add axes
         axis(1,at=-3:3)
          axis(2,at=7:16,las=2)
          #outer solid rectangle
          rect(xleft=-3,ybottom=7,xright=3,ytop=16,ity=1)
## Warning in rect(xleft = -3, ybottom = 7, xright = 3, ytop = 16, ity = 1): "ity"
## is not a graphical parameter
          # inner dashed rectangle
          rect(xleft=-2.8,ybottom=7.3,xright=2.8,ytop=15.7,ity=2)
## Warning in rect(xleft = -2.8, ybottom = 7.3, xright = 2.8, ytop = 15.7, : "ity"
## is not a graphical parameter
          # central text
          text(x=0,y=11.3,labels="SOMETHING\nPROFOUND",font=2,cex=0.75)
          #left-horizontal arrow
          arrows(x0=-2.2,y0=11.3,x1=-0.8,y1=11.3,length=0.12)
          #right horizontal arrow
          arrows(x0=2.2,y0=11.3,x1=0.8,y1=11.3,length=0.12)
        #upper-left diagonal arrow
          arrows(x0=-2.1,y0=15.1,x1=-0.8,y1=12,length=0.12)
          # upper-right diagonal arrow
          arrows(x0=2.2,y0=7.8,x1=0.8,y1=10.6,length=0.12) 
          # add an outer box
          box()

# exercise 7.1 redo 
#(a)
# Create an empty plotting area
plot(
  NA,
  xlim = c(-3, 3),
  ylim = c(7, 16),
  xlab = "",
  ylab = "",
  axes = FALSE
)

# Add axes
axis(1, at = -3:3)
axis(2, at = 7:16, las = 2)

# Outer solid rectangle
rect(
  xleft = -3,
  ybottom = 7,
  xright = 3,
  ytop = 16,
  lty = 1
)

# Inner dashed rectangle
rect(
  xleft = -2.8,
  ybottom = 7.3,
  xright = 2.8,
  ytop = 15.7,
  lty = 2
)

# Central text
text(
  x = 0,
  y = 11.3,
  labels = "SOMETHING\nPROFOUND",
  font = 2,
  cex = 0.75
)

# Left horizontal arrow
arrows(
  x0 = -2.2, y0 = 11.3,
  x1 = -0.8, y1 = 11.3,
  length = 0.12
)

# Right horizontal arrow
arrows(
  x0 = 2.2, y0 = 11.3,
  x1 = 0.8, y1 = 11.3,
  length = 0.12
)

# Upper-left diagonal arrow
arrows(
  x0 = -2.1, y0 = 15.1,
  x1 = -0.8, y1 = 12,
  length = 0.12
)

# Upper-right diagonal arrow
arrows(
  x0 = 2.1, y0 = 15.1,
  x1 = 0.8, y1 = 12,
  length = 0.12
)

# Lower-left diagonal arrow
arrows(
  x0 = -2.2, y0 = 7.8,
  x1 = -0.8, y1 = 10.6,
  length = 0.12
)

# Lower-right diagonal arrow
arrows(
  x0 = 2.2, y0 = 7.8,
  x1 = 0.8, y1 = 10.6,
  length = 0.12
)

# Add an outer plotting box
box()

#(b)
people <- data.frame(
  weight = c(55, 85, 75, 42, 93, 63, 58, 75, 89, 67),
  height = c(161, 185, 174, 154, 188, 178, 170, 167, 181, 178),
  sex = c(
    "female", "male", "male", "female", "male",
    "male", "female", "male", "male", "female"
  )
)

plot(
  people$weight,
  people$height,
  pch = ifelse(people$sex == "male", 16, 17),
  col = ifelse(people$sex == "male", "blue", "red"),
  xlab = "Weight (kg)",
  ylab = "Height (cm)",
  main = "Relationship Between Weight and Height",
  cex = 1.3
)

legend(
  "topleft",
  legend = c("Male", "Female"),
  pch = c(16, 17),
  col = c("blue", "red"),
  title = "Sex"
)

# 7.4 the ggplot2 package 
 library("ggplot2")
 foo <- c(1.1,2,3.5,3.9,4.2)
  bar <- c(2,2.2,-1.3,0,0.2)
   qplot(foo,bar)
## Warning: `qplot()` was deprecated in ggplot2 3.4.0.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

    qplot(foo,bar,main="My lovely qplot",xlab="x axis label",ylab="location y")

     baz <- plot(foo,bar)

     baz
## NULL
     qux <- qplot(foo,bar)
     qux

     # setting appearance constants with geoms
      qplot(foo,bar,geom="blank") + geom_point() + geom_line()

       qplot(foo,bar,geom="blank") + geom_point(size=3,shape=6,color="blue") +
geom_line(color="red",linetype=2)

       # the + geoms are used to modify the plotted objects in the ggplot2
        myqplot <- qplot(foo,bar,geom="blank") + geom_line(color="red",linetype=2)
         myqplot + geom_point(size=3,shape=3,color="blue")

          myqplot + geom_point(size=3,shape=7,color="blue")

          myqplot

          xee <-  myqplot + geom_point(size=3,shape=7,color="blue")
          xee

          #Aesthetic Mapping with Geoms
          x <- 1:20
           y <- c(-1.49,3.37,2.59,-2.78,-3.94,-0.92,6.43,8.51,3.41,-8.23,-12.01,-6.58,2.87,14.12,9.63,-4.58,-14.78,-11.67,1.17,15.62)
           ptype <- rep(NA,length(x=x))
            ptype[y>=5] <- "too_big"
             ptype[y<=-5] <- "too_small"
              ptype[(x>=5&x<=15)&(y>-5&y<5)] <- "sweet"
               ptype[(x<5|x>15)&(y>-5&y<5)] <- "standard"
               ptype <- factor(x=ptype) 
                ptype
##  [1] standard  standard  standard  standard  sweet     sweet     too_big  
##  [8] too_big   sweet     too_small too_small too_small sweet     too_big  
## [15] too_big   standard  too_small too_small standard  too_big  
## Levels: standard sweet too_big too_small
                 qplot(x,y,color=ptype,shape=ptype)

                 qplot(x,y,color=ptype,shape=ptype) + geom_point(size=4) +
geom_line(mapping=aes(group=1),color="black",lty=2) +
geom_hline(mapping=aes(yintercept=c(-5,5)),color="red") +
geom_segment(mapping=aes(x=5,y=-5,xend=5,yend=5),color="red",lty=3) +
geom_segment(mapping=aes(x=15,y=-5,xend=15,yend=5),color="red",lty=3) 

#READING ANDWRITINGFILES
 library(help="datasets")
?ChickWeight
## starting httpd help server ... done
 ChickWeight[1:15,]
##    weight Time Chick Diet
## 1      42    0     1    1
## 2      51    2     1    1
## 3      59    4     1    1
## 4      64    6     1    1
## 5      76    8     1    1
## 6      93   10     1    1
## 7     106   12     1    1
## 8     125   14     1    1
## 9     149   16     1    1
## 10    171   18     1    1
## 11    199   20     1    1
## 12    205   21     1    1
## 13     40    0     2    1
## 14     49    2     2    1
## 15     58    4     2    1