This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
## Chapter 2: NUMERICS,ARITHMETIC,ASSIGNMENT, AND VECTORS
## Using R, how would you calculate the square root of half of the
## average of the numbers 25.2, 15, 16.44, 15.3, and 18.6?
x<-mean(c(25.2,15,16.44,15.3,18.6))
x
## [1] 18.108
b<-sqrt(x/2)
b
## [1] 3.008987
### Find loge 03.
log(0.3) ## This is a natural log
## [1] -1.203973
log(0.3,base=10)## this is log of base 10
## [1] -0.5228787
## Computethe exponential transform of your answer
exp(-1.203973)
## [1] 0.2999999
## assigning object <-
## Create an object that stores the value 3^2 * 4^1/8
an<-3^2*4^(1/8)
an
## [1] 10.70286
## Overwrite your object in (a) by itself divided by 2.33. Print the
## result to the console.
an<- an/2.33
## Create a new object with the value −8:2 × 10−13.
new<--8.2*10^(-13)
new
## [1] -8.2e-13
## Print directly to the console the result of multiplying (b) by (c)
result<-an*new
result
## [1] -3.766673e-12
## Create and store a sequence of values from 5 to −11 that progresses ## in steps of 0.3.
baz<-seq(5,-11,by=-0.3)
baz
## [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
## Overwrite the object from (a) using the same sequence with the
## order reversed.
sort(baz)
## [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
## Repeat the vector c(-1,3,-5,7,-9) twice, with each element
## repeated 10 times, and store the result. Display the result sorted
## from largest to smallest.
bat<-rep(c(-1,3,-5,7,-9),times=2,each=10)
bat
## [1] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 3 3 3 3 3 3 3 3 3 3 -5 -5 -5 -5 -5
## [26] -5 -5 -5 -5 -5 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 -5 -5 -5 -5 -5
## [76] -5 -5 -5 -5 -5 7 7 7 7 7 7 7 7 7 7 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9
sort(bat)
## [1] -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -9 -5 -5 -5 -5 -5
## [26] -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
## [51] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
## [76] 3 3 3 3 3 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
## Create and store a vector that contains, in any configuration, the
## following:
## i. A sequence of integers from 6 to 12 (inclusive)
## ii. A threefold repetition of the value 5.3
## iii. The number −3
## iv. A sequence of nine values starting at 102 and ending at the
## number that is the total length of the vector created in (c)
gat<-c(6:12,rep(5.3,3),-3,seq(102,100,length.out=9))
gat
## [1] 6.00 7.00 8.00 9.00 10.00 11.00 12.00 5.30 5.30 5.30
## [11] -3.00 102.00 101.75 101.50 101.25 101.00 100.75 100.50 100.25 100.00
length(gat)==20
## [1] TRUE
## Chapter 3 : Matrices and Array
## Exercise
## Construct and store a 4 × 2 matrix that’s filled row-wise with the
## values 4.3, 3.1, 8.2, 8.2, 3.2, 0.9, 1.6, and 6.5, in that order.
mat<-matrix(data=c(4.3,3.1,8.2,8.2,3.2,0.9,1.6,6.5),nrow=4,ncol=2,byrow=TRUE)
mat
## [,1] [,2]
## [1,] 4.3 3.1
## [2,] 8.2 8.2
## [3,] 3.2 0.9
## [4,] 1.6 6.5
## Confirm the dimensions of the matrix from (a) are 3 × 2 if you
## remove any one row.
hat<-mat[-1,]
hat
## [,1] [,2]
## [1,] 8.2 8.2
## [2,] 3.2 0.9
## [3,] 1.6 6.5
dim(hat)
## [1] 3 2
# c.
mat[,2]<-sort(mat[,2])## overwrite the second column of the matrix and sort it with the same column starting from the smallest to largest
mat
## [,1] [,2]
## [1,] 4.3 0.9
## [2,] 8.2 3.1
## [3,] 3.2 6.5
## [4,] 1.6 8.2
# d.
matrix(mat[-4,-1])## delete row 4 and column 1 and extract the remaining column
## [,1]
## [1,] 0.9
## [2,] 3.1
## [3,] 6.5
mat
## [,1] [,2]
## [1,] 4.3 0.9
## [2,] 8.2 3.1
## [3,] 3.2 6.5
## [4,] 1.6 8.2
# e.
x<-hat[c(2,3),c(1,2)]
x
## [,1] [,2]
## [1,] 3.2 0.9
## [2,] 1.6 6.5
dim(x)
## [1] 2 2
#f
mat[c(4,1),c(2,1)]<- -0.5*diag(x)
mat
## [,1] [,2]
## [1,] -3.25 -3.25
## [2,] 8.20 3.10
## [3,] 3.20 6.50
## [4,] -1.60 -1.60
# matrix operation and algebra
A <- rbind(c(2,5,2),c(6,1,4))
t(A)
## [,1] [,2]
## [1,] 2 6
## [2,] 5 1
## [3,] 2 4
## scalar multiple of a matrix
sca<-matrix(data=c(2,5,2,6,1,4),byrow=TRUE,nrow=2,ncol=3)
sca
## [,1] [,2] [,3]
## [1,] 2 5 2
## [2,] 6 1 4
a<-2
a*sca
## [,1] [,2] [,3]
## [1,] 4 10 4
## [2,] 12 2 8
## matrix addition and subtraction
A<- cbind(c(2,5,2),c(6,1,4))
A
## [,1] [,2]
## [1,] 2 6
## [2,] 5 1
## [3,] 2 4
B<- cbind(c(4,2,-4),c(-2.1,-7.2,14.8))
B
## [,1] [,2]
## [1,] 4 -2.1
## [2,] 2 -7.2
## [3,] -4 14.8
A-B
## [,1] [,2]
## [1,] -2 8.1
## [2,] 3 8.2
## [3,] 6 -10.8
## matrix multiplication
d<-rbind(c(2,5,2),c(6,1,4))
d
## [,1] [,2] [,3]
## [1,] 2 5 2
## [2,] 6 1 4
e<-cbind(c(3,-1,1),c(-3,1,5))
e
## [,1] [,2]
## [1,] 3 -3
## [2,] -1 1
## [3,] 1 5
###
A<-matrix(data=c(3,4,1,2),nrow=2,ncol=2)
A
## [,1] [,2]
## [1,] 3 1
## [2,] 4 2
solve(A) ## to find the inverse
## [,1] [,2]
## [1,] 1 -0.5
## [2,] -2 1.5
## Exercise
## Create and store a three-dimensional array with six layers of a
## 4 × 2 matrix, filled with a decreasing sequence of values between
## 4.8 and 0.1 of the appropriate length.
var<- array(data=seq(from=4.8,to=0.1,length.out=48),dim=c(4,2,6))
var
## , , 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
## Extract and store as a new object the fourth- and first-row ## elements, in that order, of the second column only of all layers(b)
newh<- var[c(4,1),2,]
newh
## [,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
## Use a fourfold repetition of the second row of the matrix formed
## in (b) to fill a new array of dimensions 2 × 2 × 2 × 3.
array(data=rep(newh[2,],times=4),dim = c(2,2,2,3))
## , , 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
## Chapter 4 : Non Numeric values
## factors
firstname <- c("Liz","Jolene","Susan","Boris","Rochelle","Tim","Simon",
"Amy")
sex.num <- c(0,0,0,1,0,1,1,0)
sex.char <- c("female","female","female","male","female","male","male",
"female")
sex.num.fac <- factor(x=sex.num)
sex.num.fac
## [1] 0 0 0 1 0 1 1 0
## Levels: 0 1
sex.char.fac <- factor(x=sex.char)
sex.char.fac
## [1] female female female male female male male female
## Levels: female male
levels(x=sex.num.fac)
## [1] "0" "1"
levels(x=sex.char.fac)
## [1] "female" "male"
levels(x=sex.num.fac) <- c("1","2")
sex.num.fac
## [1] 1 1 1 2 1 2 2 1
## Levels: 1 2
sex.char.fac[2:5]
## [1] female female male female
## Levels: female male
sex.char.fac[c(1:3,5,8)]
## [1] female female female female female
## Levels: female male
## defining and ordering levels
mob <- c("Apr","Jan","Dec","Sep","Nov","Jul","Jul","Jun")
mob[2]
## [1] "Jan"
mob[3]
## [1] "Dec"
mob[2]<mob[3]
## [1] FALSE
ms <- c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov",
"Dec")
mob.fac <- factor(x=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
mob.fac[2]
## [1] Jan
## 12 Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < ... < Dec
mob.fac[3]
## [1] Dec
## 12 Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < ... < Dec
mob.fac[2]<mob.fac[3]
## [1] TRUE
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(x=c("Oct","Feb","Feb"),levels=levels(mob.fac),
ordered=TRUE)
c(mob.fac,new.values)
## [1] Apr Jan Dec Sep Nov Jul Jul Jun Oct Feb Feb
## 12 Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < ... < Dec
as.integer(c(mob.fac,new.values))
## [1] 4 1 12 9 11 7 7 6 10 2 2
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"
mob.new <- levels(mob.fac)[c(mob.fac,new.values)]
mob.new.fac <- factor(x=mob.new,levels=levels(mob.fac),ordered=TRUE)
## cut
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(x=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(x=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)
cut(x=Y,breaks=br,right=FALSE,include.lowest=TRUE)
## [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(x=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
## Store the following vector of 15 values as an object in your
## workspace: c(6,9,7,3,6,7,9,6,3,6,6,7,1,9,1). Identify the following ## elements:
## i. Those equal to 6
## ii. Those greater than or equal to 6
## iii. Those less than 6 + 2
## iv. Those not equal to 6
yat<-c(6,9,7,3,6,7,9,6,3,6,6,7,1,9,1)
yat
## [1] 6 9 7 3 6 7 9 6 3 6 6 7 1 9 1
length(yat)
## [1] 15
yat==6
## [1] TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE FALSE
## [13] FALSE FALSE FALSE
yat>=6
## [1] TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE
## [13] FALSE TRUE FALSE
yat<(6+2)
## [1] TRUE FALSE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE
## [13] TRUE FALSE TRUE
yat!=6
## [1] FALSE TRUE TRUE TRUE FALSE TRUE TRUE FALSE TRUE FALSE FALSE TRUE
## [13] TRUE TRUE TRUE
## Store the vector c(7,1,7,10,5,9,10,3,10,8) as foo. Identify the
## elements greater than 5 OR equal to 2.(a)
foo<-c(7,1,7,10,5,9,10,3,10,8)
foo
## [1] 7 1 7 10 5 9 10 3 10 8
foo[(foo>5)|(foo=2)]
## [1] 7 1 7 10 5 9 10 3 10 8
## Store the vector c(8,8,4,4,5,1,5,6,6,8) as bar. Identify the ## elements less than or equal to 6 AND not equal to 4 (b)
bar<-c(8,8,4,45,1,5,6,6,8)
bar
## [1] 8 8 4 45 1 5 6 6 8
bar[(bar<=6)&(bar!=4)]
## [1] 1 5 6 6
## Identify the elements that satisfy (a) in foo AND satisfy (b) in bar
(foo>5)|(foo=2)&(bar<=6)&(bar!=4)
## [1] FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE
## Store this vector of 10 values: foo <- c(7,5,6,1,2,10,8,3,8,2).
## Then, do the following:
## i. Extract the elements greater than or equal to 5, storing the
## result as bar.
## ii. Display the vector containing those elements from foo that
## remain after omitting all elements that are greater than or
## equal to 5.
foo<-c(7,5,6,1,2,10,8,3,8,2)
foo
## [1] 7 5 6 1 2 10 8 3 8 2
bar<-foo[foo>=5]
## (ii)
gat<-foo[foo<5]
gat
## [1] 1 2 3 2
## Use bar from (a)(i) to construct a 2 × 3 matrix called baz, filled ## in
## a row-wise fashion. Then, do the following:
## i. Replace any elements that are equal to 8 with the squared
## value of the element in row 1, column 2 of baz itself.
## ii. Confirm that all values in baz are now less than or equal to 25
## AND greater than 4.
baz<- matrix(data=bar,nrow=2,ncol=3,byrow=TRUE)
baz
## [,1] [,2] [,3]
## [1,] 7 5 6
## [2,] 10 8 8
# b(i)
baz[baz==8]<-baz[1,2]*2
## b(ii)
all(baz<=25 & baz>4)
## [1] TRUE
## Create a 3 × 2 × 3 array called qux using the following vector of
## 18 values: c(10,5,1,4,7,4,3,3,1,3,4,3,1,7,8,3,7,3). Then, do the
## following:
## i. Identify the dimension-specific index positions of elements
## that are either 3 OR 4.
## ii. Replace all elements in qux that are less than 3 OR greater
## than or equal to 7 with the value 100.
qux<-c(10,5,1,4,7,4,3,3,1,3,4,3,1,7,8,3,7,3)
qux
## [1] 10 5 1 4 7 4 3 3 1 3 4 3 1 7 8 3 7 3
length(qux)
## [1] 18
# c (i)
qux<-array(data=qux,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
## c(i)
matches<-which(qux==3|qux==4,arr.ind=TRUE)
matches
## 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
## c(ii)
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
## concatenation
qux<-c("awesome","R","is")
qux
## [1] "awesome" "R" "is"
length(qux)
## [1] 3
nchar(qux)
## [1] 7 1 2
cat(qux[2],qux[3],"totally",qux[1],"!")## cannot assign the result to a new variable
## R is totally awesome !
paste(qux[2],qux[3],"totally",qux[1],"!")## for the result to be in quote means it can be assigned to an object
## [1] "R is totally awesome !"
paste(qux[2],qux[3],"totally",qux[1],"!",sep="---") ## sep means to separate a character string
## [1] "R---is---totally---awesome---!"
paste(qux[2],qux[3],"totally",qux[1],"!",sep="")
## [1] "Ristotallyawesome!"
cat("Do you think",qux[2]," ",qux[3],qux[1],"?")
## Do you think R is 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] "Is 7.4 less than 10? That's totally TRUE ."
cat("here is a string\n split\n\t to new\b\n\n\tlines")
## here is a string
## split
## to new
##
## lines
cat("I really want a backslash:\\\nand a double:\"")
## I really want a backslash:\
## and a double:"
## substring and matching
foo<-"This is a character string!"
substr(x=foo,start=21,stop=27)
## [1] "string!"
substr(x=foo,start=1,stop=4)<-"Here"
foo
## [1] "Here is a character string!"
bar<-"How much wood could a woodchuck chuck"
sub(pattern="chuck",replacement="hurl",x=bar)## it replace the first instance with a new string
## [1] "How much wood could a woodhurl chuck"
gsub(pattern="chuck",replacement="hurl",x=bar) ## it replace every instance of pattern
## [1] "How much wood could a woodhurl hurl"
## Exercise:Re-create exactly the following output:
## "The quick brown fox
## jumped over
## the lazy dogs"
cat("\"The quick brown fox \n\tjumped over\n\t\tthe lazy dogs\"") ##\ use for including double quotes
## "The quick brown fox
## jumped over
## the lazy dogs"
# Suppose you’ve stored the values num1 <- 4 and num2 <- 0.75.
## Write a line of R code that returns the following string:
## [1] "The result of multiplying 4 by 0.75 is 3"
## Make sure your code produces a string with the correct
## multiplication result for any two numbers stored as num1 and num2
num1<-4
num2<-0.75
paste("The result of multiplying",num1,"by",num2,"is",num1*num2)
## [1] "The result of multiplying 4 by 0.75 is 3"
cat("The result of multiplying",num1,"by",num2,"is",num1*num2)
## The result of multiplying 4 by 0.75 is 3
##
bar<-"How much wood could a woodchuck chuck"
## i
glu<-paste(bar,"if a woodchuck could chuck wood")
glu
## [1] "How much wood could a woodchuck chuck if a woodchuck could chuck wood"
## ii
gsub(pattern="wood",replacement="metal",bar)
## [1] "How much metal could a metalchuck chuck"
## Store the string "Two 6-packs for $12.99". Then do the following:
## i. Use a check for equality to confirm that the substring
## beginning with character 5 and ending with character 10
## is "6-pack".
## ii. Make it a better deal by changing the price to $10.99.
pack<-"Two 6-packs for $12.99"
pack
## [1] "Two 6-packs for $12.99"
## i
substr(start=5,stop=10,pack)=="6-pack"
## [1] TRUE
sub(pattern="\\$12.99",replacement="$10.99",pack)
## [1] "Two 6-packs for $10.99"
## Chapter 5: Lists and Data Frames
## Create a list that contains, in this order, a sequence of 20 evenly
## spaced numbers between −4 and 4; a 3 × 3 matrix of the logical
## vector c(F,T,T,T,F,T,T,F,F) filled column-wise; a character vector
##with the two strings "don" and "quixote"; and a factor vector containing the ## observations c("LOW","MED","LOW","MED","MED","HIGH").
## Then, do the following:
## i. Extract row elements 2 and 1 of columns 2 and 3, in that
## order, of the logical matrix.
## ii. Use sub to overwrite "quixote" with "Quixote" and "don" with
## "Don" inside the list. Then, using the newly overwritten list
## member, concatenate to the console screen the following
## statement exactly:
## "Windmills! ATTACK!"
## -\Don Quixote/-
## iii. Obtain all values from the sequence between −4 and 4 that
## are greater than 1.
## iv. Using which, determine which indexes in the factor vector are
## assigned the "MED" level.
bar<-list(seq(-4,4,length.out=20), matrix(data=c(F,T,T,T,F,T,T,F,F),nrow=3, ncol=3), harry=c("don","quixiote"), factor(c("LOW","MED","LOW","MED","MED","HIGH")))
bar
## [[1]]
## [1] -4.0000000 -3.5789474 -3.1578947 -2.7368421 -2.3157895 -1.8947368
## [7] -1.4736842 -1.0526316 -0.6315789 -0.2105263 0.2105263 0.6315789
## [13] 1.0526316 1.4736842 1.8947368 2.3157895 2.7368421 3.1578947
## [19] 3.5789474 4.0000000
##
## [[2]]
## [,1] [,2] [,3]
## [1,] FALSE TRUE TRUE
## [2,] TRUE FALSE FALSE
## [3,] TRUE TRUE FALSE
##
## $harry
## [1] "don" "quixiote"
##
## [[4]]
## [1] LOW MED LOW MED MED HIGH
## Levels: HIGH LOW MED
bar[[2]][c(2,1),c(2,3)]
## [,1] [,2]
## [1,] FALSE FALSE
## [2,] TRUE TRUE
## a(ii)
bar[[3]]<-sub(pattern="quixiote",replacement = "Quixiote",x=bar[[3]])
bar[[3]]<- sub(pattern="don",replacement="Don",x=bar[[3]])
bar[[3]]
## [1] "Don" "Quixiote"
cat("\"Windmills! ATTACK!\"\n\t-\\Don Quixote/-")
## "Windmills! ATTACK!"
## -\Don Quixote/-
## a(iii)
valu<- -4:4
valu[valu>1]
## [1] 2 3 4
## a(iv)
which(bar[[4]]=="MED") ## INDEXES IN THE FACTOR EQUAL MED
## [1] 2 4 5
## Create a new list with the factor vector from (a) as a component named "facs"; the ## numeric vector c(3,2.1,3.3,4,1.5,4.9) as a
## component named "nums"; and a nested list comprised of the first
## three members of the list from (a) (use list slicing to obtain this),
## named "oldlist". Then, do the following:
## i. Extract the elements of "facs" that correspond to elements of
## "nums" that are greater than or equal to 3.
## ii. Add a new member to the list named "flags". This member
## should be a logical vector of length 6, obtained as a twofold
## repetition of the third column of the logical matrix in the
## "oldlist" component.
## iii. Use "flags" and the logical negation operator ! to extract the
## entries of "num" corresponding to FALSE.
## iv. Overwrite the character string vector component of "oldlist"
## with the single character string "Don Quixote".
facs=bar[[4]]
nums<-c(3,2.1,3.3,4,1.5,4.9)
oldlist<-bar[1:3]
newlist<-list(nums=c(3,2.1,3.3,4,1.5,4.9),
oldlist=bar[1:3],
facs=bar[[4]])
newlist
## $nums
## [1] 3.0 2.1 3.3 4.0 1.5 4.9
##
## $oldlist
## $oldlist[[1]]
## [1] -4.0000000 -3.5789474 -3.1578947 -2.7368421 -2.3157895 -1.8947368
## [7] -1.4736842 -1.0526316 -0.6315789 -0.2105263 0.2105263 0.6315789
## [13] 1.0526316 1.4736842 1.8947368 2.3157895 2.7368421 3.1578947
## [19] 3.5789474 4.0000000
##
## $oldlist[[2]]
## [,1] [,2] [,3]
## [1,] FALSE TRUE TRUE
## [2,] TRUE FALSE FALSE
## [3,] TRUE TRUE FALSE
##
## $oldlist$harry
## [1] "Don" "Quixiote"
##
##
## $facs
## [1] LOW MED LOW MED MED HIGH
## Levels: HIGH LOW MED
## b (i)
newlist$facs[nums>=3]
## [1] LOW LOW MED HIGH
## Levels: HIGH LOW MED
## b (ii)
newlist$flag <- rep(x=newlist$oldlist[[2]][,3],length.out=6)
newlist
## $nums
## [1] 3.0 2.1 3.3 4.0 1.5 4.9
##
## $oldlist
## $oldlist[[1]]
## [1] -4.0000000 -3.5789474 -3.1578947 -2.7368421 -2.3157895 -1.8947368
## [7] -1.4736842 -1.0526316 -0.6315789 -0.2105263 0.2105263 0.6315789
## [13] 1.0526316 1.4736842 1.8947368 2.3157895 2.7368421 3.1578947
## [19] 3.5789474 4.0000000
##
## $oldlist[[2]]
## [,1] [,2] [,3]
## [1,] FALSE TRUE TRUE
## [2,] TRUE FALSE FALSE
## [3,] TRUE TRUE FALSE
##
## $oldlist$harry
## [1] "Don" "Quixiote"
##
##
## $facs
## [1] LOW MED LOW MED MED HIGH
## Levels: HIGH LOW MED
##
## $flag
## [1] TRUE FALSE FALSE TRUE FALSE FALSE
## b(iii)
newlist$nums[!newlist$flag]
## [1] 2.1 3.3 1.5 4.9
## b(iv)
newlist$oldlist[[3]]<-"Don Quixote"
## Data Frame
mydata <- data.frame(person=c("Peter","Lois","Meg","Chris","Stewie"),
age=c(42,40,17,14,1),
sex=factor(c("M","F","F","M","M")))
mydata
## person age sex
## 1 Peter 42 M
## 2 Lois 40 F
## 3 Meg 17 F
## 4 Chris 14 M
## 5 Stewie 1 M
### Chapter 6 : Special Values,classes and coercion
## Store the following vector:
##foo <- c(13563,-14156,-14319,16981,12921,11979,9568,8833,-12968,8133)
## Then, do the following:
## i. Output all elements of foo that, when raised to a power of 75,
## are NOT infinite.
##ii. Return the elements of foo, excluding those that result in
## negative infinity when raised to a power of 75.
foo <- c(13563,-14156,-14319,16981,12921,11979,9568,8833,-12968,8133)
foo[!is.infinite(foo^75)]
## [1] 11979 9568 8833 8133
foo[is.finite(foo^75)]
## [1] 11979 9568 8833 8133
## Consider the following line of code:
## foo <- c(4.3,2.2,NULL,2.4,NaN,3.3,3.1,NULL,3.4,NA)
## Decide yourself which of the following statements are true
## and which are false and then use R to confirm:
## i. The length of foo is 8.
## ii. Calling which(x=is.na(x=foo)) will not result in 4 and 8.
## iii. Checking is.null(x=foo) will provide you with the locations of
## the two NULL values that are present.
## iv. Executing is.na(x=foo[8])+4/NULL will not result in NA.
foo<-c(4.3,2.2,NULL,2.4,NaN,3.3,3.1,NULL,34,NA)
foo
## [1] 4.3 2.2 2.4 NaN 3.3 3.1 34.0 NA
## a(i)
length(foo) ==8
## [1] TRUE
## a(ii)
which(x=is.na(x=foo))!= c(4,8)
## [1] FALSE FALSE
## a(iii)
which(x=is.null(x=foo))
## integer(0)
## a(iv)
is.na(x=foo[8])+4/NULL
## numeric(0)
### Chapter 7: Basic Plotting
bar<-c(2,2.2,-1.3,0,0.2)
foo<-c(1.1,2,3.5,3.9,4.2)
plot(foo,bar)
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)
##
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<-factor(c("female","male","male","female","male","male","female","male","male","female"))
sex
## [1] female male male female male male female male male female
## Levels: female male
color_1<-c("female"="blue","male"="red")
color<-color_1[sex]
plot(weight,height,
xlab="Weight(kg)",ylab="Height(cm)",
col=color,pch=18)
legend("topleft",legend=c("female","male"),col=color_1,pch=18)
## Chapter 8: Reading and Writing Files
library(help="datasets")
mydata2<-read.table(file="/Users/USER/OneDrive/Desktop/R CLASS/8.2.1_mydatafile.txt",header=TRUE,sep=" ",na.strings="*",stringsAsFactor=FALSE)
mydata2
## person age sex funny age.mon
## 1 Peter NA M High 504
## 2 Lois 40 F <NA> 480
## 3 Meg 17 F Low 204
## 4 Chris 14 M Med 168
## 5 Stewie 1 M High NA
## 6 Brian NA M Med NA
write.table(x=mydata2,file="C:/Users/USER/OneDrive/Documents/somenewfile.txt ",sep="@",na="??",quote=FALSE,row.names=FALSE)
read.table("somenewfile.txt")
## V1
## 1 person@age@sex@funny@age.mon
## 2 Peter@??@M@High@504
## 3 Lois@40@F@??@480
## 4 Meg@17@F@Low@204
## 5 Chris@14@M@Med@168
## 6 Stewie@1@M@High@??
## 7 Brian@??@M@Med@??
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.