## [,1] [,2]
## [1,] 4.3 3.1
## [2,] 8.2 8.2
## [3,] 3.2 0.9
## [4,] 1.6 6.5
## [1] 3 2
## [,1] [,2]
## [1,] 4.3 0.9
## [2,] 8.2 3.1
## [3,] 3.2 6.5
## [4,] 1.6 8.2
## [1] 0.9 3.1 6.5
## [,1]
## [1,] 0.9
## [2,] 3.1
## [3,] 6.5
## [,1] [,2]
## [1,] 3.2 6.5
## [2,] 1.6 8.2
## [,1] [,2]
## [1,] -4.1 -4.1
## [2,] 8.2 3.1
## [3,] 3.2 6.5
## [4,] -1.6 -1.6
## [,1] [,2]
## [1,] -3 893.00
## [2,] 2 0.17
## [,1]
## [1,] -3.00
## [2,] 2.00
## [3,] 893.00
## [4,] 0.17
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [,1] [,2]
## [1,] 1 3
## [2,] 4 6
## [,1] [,2] [,3]
## [1,] 1 3 4
## [2,] 5 4 3
## [3,] 100 20 90
## [4,] 11 12 13
## [1] 4 3
## [1] 4
## [1] 3
## [1] 3
## [,1] [,2]
## [1,] -3 893.00
## [2,] 3 0.17
## [,1]
## [1,] -3.00
## [2,] 2.00
## [3,] 893.00
## [4,] 0.17
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [1] 7 5 6 1 2 10 8 3 8 2
## [1] TRUE TRUE TRUE FALSE FALSE TRUE TRUE FALSE TRUE FALSE
## [1] 7 5 6 10 8 8
## [1] 1 2 3 2
## [,1] [,2] [,3]
## [1,] 7 5 6
## [2,] 10 8 8
## [,1] [,2] [,3]
## [1,] 7 5 6
## [2,] 10 25 25
## [1] TRUE
## [1] TRUE
## [1] "awesome" "R" "is"
## [1] 3
## [1] 7 1 2
## R is awesome !
## [1] "R is totally awesome !"
## [1] "R---is---totally---awesome---!"
## [1] "Ristotallyawesome!"
## [1] "R is totally awesome !"
## [1] "R//is//totally//awesome//!"
## Do you think R is awesome?
## [1] "Do you think"
## [1] "Do you think R is awesome?"
## [1] 3
## [1] 4.4
## The value stored as 'a' is 3.
## [1] "The value stored as 'a' is 4.4."
## The result of a+b is 3+4.4=7.4.
## [1] "Is 7.4 less than 10? That's totallyTRUE."
## here is a string
## split to neww
##
## lines
## here is a string
## split to neww
##
## lines
## here is a string
## split to neww
## lines
## I really want a blackslash: \nand a double quote: "
## I really want a blackslash: \
## and a double quote: "
## [1] "I really want a blackslash: \\\nand a double quote: \""
#####EXERCISE 5.1i
fav<-list(seq(-4,4, length.out=20),matrix(data = c(F,T,T,T,F,T,T,F,F),nrow=3, ncol=3,
byrow=FALSE),String=c("don","quixote"),factor(c("LOW","MED","LOW","MED","MED","HIGH")))
favvy<-fav[[2]]
favie<-favvy[c(2,1),c(2,3)]
favie ##OR## [,1] [,2]
## [1,] FALSE FALSE
## [2,] TRUE TRUE
## [,1] [,2]
## [1,] FALSE FALSE
## [2,] TRUE TRUE
## [[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
##
## $String
## [1] "don" "Quixote"
##
## [[4]]
## [1] LOW MED LOW MED MED HIGH
## Levels: HIGH LOW MED
## [[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
##
## $String
## [1] "Don" "Quixote"
##
## [[4]]
## [1] LOW MED LOW MED MED HIGH
## Levels: HIGH LOW MED
## "windmills! ATTACK!
## -\Don Quixote/-
## [1] 2 4 5
## [1] 2 4 5
## $facs
## [1] LOW MED LOW MED MED HIGH
## Levels: HIGH LOW MED
##
## $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$String
## [1] "Don" "Quixote"
## [1] LOW LOW MED HIGH
## Levels: HIGH LOW MED
## [1] TRUE FALSE FALSE TRUE FALSE FALSE
## $facs
## [1] LOW MED LOW MED MED HIGH
## Levels: HIGH LOW MED
##
## $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$String
## [1] "Don" "Quixote"
##
##
## $flags
## [1] TRUE FALSE FALSE TRUE FALSE FALSE
## [1] 2.1 3.3 1.5 4.9
newlist$oldlist$String<-"Don Quixote"
# Exercise 5.2d
mydata<-data.frame(person=c("Peter","Lois","Meg","Chris","Stewie","Brian"),
age=c(42,40,17,14,1,7),
sex=factor(c("M","F","F","M","M","M")),
funny=factor(c("High","High","Low","Med","High","Med"),
levels = c("Low","Med","High"),ordered = T),
age.mon=c(504,480,204,168,12,84),stringsAsFactors = F)
mydata## person age sex funny age.mon
## 1 Peter 42 M High 504
## 2 Lois 40 F High 480
## 3 Meg 17 F Low 204
## 4 Chris 14 M Med 168
## 5 Stewie 1 M High 12
## 6 Brian 7 M Med 84
## person age sex funny
## 1 Peter 42 M High
## 2 Lois 40 F High
## 3 Meg 17 F Low
## 4 Chris 14 M Med
## 5 Stewie 1 M High
## 6 Brian 7 M Med
## person age sex funny
## 1 Peter 42 M High
## 2 Lois 40 F High
## 3 Meg 17 F Low
## 4 Chris 14 M Med
## 5 Stewie 1 M High
## 6 Brian 7 M Med
## person age
## 2 Lois 40
#g
nameswiths<-mydataframe[substr(mydataframe$person,start=0,stop=1)=="S",1:ncol(mydataframe)]
nameswiths## person age sex funny
## 5 Stewie 1 M High
## person age sex funny
## 5 Stewie 1 M High
##6.1.3 NA
## [1] "character" "a" NA "with" "string" NA
## [1] blue <NA> <NA> blue green blue <NA> red red <NA> green
## Levels: blue green red
## [,1] [,2] [,3]
## [1,] 1 NA NA
## [2,] 2 5 8
## [3,] 3 6 NA
## [1] NA 5.89 Inf NA 9.43 -2.35 NaN 2.10 -8.53 -7.58 NA -4.58
## [13] 2.01 NaN
## [1] TRUE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE TRUE FALSE
## [13] FALSE TRUE
## [1] 7 14
## [1] 1 4 11
## [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"
## [1] NA
## [1] 3 6 NA NA NaN 18
## [1] NA
## [1] NA
## [1] 2 2 3 5
## [1] "Definitely FoO: 34; definitely bAR: TRUE."
## [1] 1 0 0 1
## [1] 2 2 3 5
## [1] "34"
## [1] TRUE
## [1] "TRUE"
## [1] "Definitely Foo: 34; definitely BAr: TRUE."
## Warning: NAs introduced by coercion
## [1] NA
## [1] NA NA NA NA NA
## [1] TRUE FALSE TRUE FALSE FALSE
## [1] male male female male
## Levels: female male
## [1] 2 2 1 2
## [1] 2 2 3 5
## Levels: 2 3 5
## [1] 1 1 2 3
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
## [1] 1 2 3 4
## , , 1
##
## [,1] [,2] [,3]
## [1,] 8 9 5
## [2,] 1 5 1
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 3 3 8
## [2,] 4 9 8
## [,1]
## [1,] 8
## [2,] 1
## [3,] 9
## [4,] 5
## [5,] 5
## [6,] 1
## [7,] 3
## [8,] 4
## [9,] 3
## [10,] 9
## [11,] 8
## [12,] 8
## [1] 8 1 9 5 5 1 3 4 3 9 8 8
#7.3 Adding Points, Lines, and Text to an Existing Plot
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)
x[y<=-5]## [1] 10 11 12 17 18
## [1] -8.23 -12.01 -6.58 -14.78 -11.67
plot(x,y,type = "n",main = "")
abline(h=c(-5,5),col="red",lty=2,lwd=2)
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),cex = 0.5)##Exercise 7.1
plot(-3:3, 7:13, type="n", xlab="", ylab="")
text(x=0, y=10, labels="SOMETHING\nPROFOUND")
abline(v=c(-3,3), lty=2, lwd=3, col=8)
abline(h=c(7,13), lty=2, lwd=3, col=8)
arrows(x0=c(-3,-3,-3,3,3,3),
y0=c(7,10,13,7,10,13),
x1=c(-1,-1,-1,1,1,1),
y1=c(9.5,10,10.5,9.5,10,10.5),
col=8, lwd=3)#Exercise 7.1b
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(Weight,Height)colour_1<-c("male"="blue","female"="red")
colors<-colour_1[sex]
pch_map <- c("male" = 16, "female" = 17)
pch_vals <- pch_map[sex]
plot(Weight,Height,
col=colors,
pch=pch_vals,
xlab = "Weight(kg)",
ylab = "Height(cm)",
main= "Height vs. weight by Sex")
legend("topleft",legend=c("male","female"),col=c("blue","red"),pch=c(16,17))#Additional tools to refine and annotate plots:
sex <- factor(c("male", "female", "male", "female"))
height <- c(180, 165, 175, 160)
plot(sex, height)
text(x = 1, y = 180, labels = "Tall Male")
abline(h = 170, col = "red", lty = 2)
legend("bottomright", legend = c("Male", "Female"), col = c("blue", "red"), pch = c(16, 17))
###CHAPTER9
## [1] 9
## [1] "stringtastic"
## [1] "a" "A" "b" "B" "ban"
## [6] "bank" "bar" "baR" "bAR" "BAr"
## [11] "BAr.ch" "bat" "baz" "BAz" "BAZ"
## [16] "colors" "colour_1" "fat" "fav" "favie"
## [21] "favvy" "foo" "fOO" "Foo" "FoO"
## [26] "Foo.ch" "HE" "height" "Height" "HEY"
## [31] "HI" "HIP" "ka" "mydata" "mydata2"
## [36] "mydataframe" "mymat" "nameswiths" "newlist" "pch_map"
## [41] "pch_vals" "qax" "quux" "qux" "QUX"
## [46] "sex" "we" "Weight" "x" "y"
## [1] "abline" "arrows" "assocplot" "axis"
## [5] "Axis" "axis.Date" "axis.POSIXct" "axTicks"
## [9] "barplot" "barplot.default" "box" "boxplot"
## [13] "boxplot.default" "boxplot.matrix" "bxp" "cdplot"
## [17] "clip" "close.screen" "co.intervals" "contour"
## [21] "contour.default" "coplot" "curve" "dotchart"
## [25] "erase.screen" "filled.contour" "fourfoldplot" "frame"
## [29] "grconvertX" "grconvertY" "grid" "hist"
## [33] "hist.default" "identify" "image" "image.default"
## [37] "layout" "layout.show" "lcm" "legend"
## [41] "lines" "lines.default" "locator" "matlines"
## [45] "matplot" "matpoints" "mosaicplot" "mtext"
## [49] "pairs" "pairs.default" "panel.smooth" "par"
## [53] "persp" "pie" "plot" "plot.default"
## [57] "plot.design" "plot.function" "plot.new" "plot.window"
## [61] "plot.xy" "points" "points.default" "polygon"
## [65] "polypath" "rasterImage" "rect" "rug"
## [69] "screen" "segments" "smoothScatter" "spineplot"
## [73] "split.screen" "stars" "stem" "strheight"
## [77] "stripchart" "strwidth" "sunflowerplot" "symbols"
## [81] "text" "text.default" "title" "xinch"
## [85] "xspline" "xyinch" "yinch"
## [1] "abline" "arrows" "assocplot" "axis"
## [5] "Axis" "axis.Date" "axis.POSIXct" "axTicks"
## [9] "barplot" "barplot.default" "box" "boxplot"
## [13] "boxplot.default" "boxplot.matrix" "bxp" "cdplot"
## [17] "clip" "close.screen" "co.intervals" "contour"
## [21] "contour.default" "coplot" "curve" "dotchart"
## [25] "erase.screen" "filled.contour" "fourfoldplot" "frame"
## [29] "grconvertX" "grconvertY" "grid" "hist"
## [33] "hist.default" "identify" "image" "image.default"
## [37] "layout" "layout.show" "lcm" "legend"
## [41] "lines" "lines.default" "locator" "matlines"
## [45] "matplot" "matpoints" "mosaicplot" "mtext"
## [49] "pairs" "pairs.default" "panel.smooth" "par"
## [53] "persp" "pie" "plot" "plot.default"
## [57] "plot.design" "plot.function" "plot.new" "plot.window"
## [61] "plot.xy" "points" "points.default" "polygon"
## [65] "polypath" "rasterImage" "rect" "rug"
## [69] "screen" "segments" "smoothScatter" "spineplot"
## [73] "split.screen" "stars" "stem" "strheight"
## [77] "stripchart" "strwidth" "sunflowerplot" "symbols"
## [81] "text" "text.default" "title" "xinch"
## [85] "xspline" "xyinch" "yinch"
## Local Environments
youthspeak<-matrix(data =c("OMG","LOL","WTF","YOLO"),nrow=2,ncol=2)
youthspeak## [,1] [,2]
## [1,] "OMG" "WTF"
## [2,] "LOL" "YOLO"
## [1] ".GlobalEnv" "package:stats" "package:graphics"
## [4] "package:grDevices" "package:utils" "package:datasets"
## [7] "package:methods" "Autoloads" "package:base"
## [1] 0.00 0.75 1.50 2.25 3.00
## <environment: namespace:base>
## <environment: namespace:graphics>
## Loading required package: carData
## [1] ".GlobalEnv" "package:car" "package:carData"
## [4] "package:stats" "package:graphics" "package:grDevices"
## [7] "package:utils" "package:datasets" "package:methods"
## [10] "Autoloads" "package:base"
## this is confusing
## [1] 42
## [1] TRUE
## [1] TRUE
## [1] "a" "A" "b" "B" "ban"
## [6] "bank" "bar" "baR" "bAR" "BAr"
## [11] "BAr.ch" "bat" "baz" "BAz" "BAZ"
## [16] "colors" "colour_1" "F" "False" "fat"
## [21] "fav" "favie" "favvy" "foo" "fOO"
## [26] "Foo" "FoO" "Foo.ch" "HE" "height"
## [31] "Height" "HEY" "HI" "HIP" "ka"
## [36] "mydata" "mydata2" "mydataframe" "mymat" "nameswiths"
## [41] "nan" "newlist" "pch_map" "pch_vals" "qax"
## [46] "quux" "qux" "QUX" "sex" "T"
## [51] "we" "Weight" "x" "y" "youthspeak"
## character(0)
## [1] "addNextMethod" "allNames"
## [3] "Arith" "as"
## [5] "as<-" "asMethodDefinition"
## [7] "assignClassDef" "assignMethodsMetaData"
## [9] "balanceMethodsList" "body<-"
## [11] "cacheGenericsMetaData" "cacheMetaData"
## [13] "cacheMethod" "callGeneric"
## [15] "callNextMethod" "canCoerce"
## [17] "cbind2" "checkAtAssignment"
## [19] "checkSlotAssignment" "classesToAM"
## [21] "classLabel" "classMetaName"
## [23] "className" "coerce"
## [25] "coerce<-" "Compare"
## [27] "completeClassDefinition" "completeExtends"
## [29] "completeSubclasses" "Complex"
## [31] "conformMethod" "defaultDumpName"
## [33] "defaultPrototype" "doPrimitiveMethod"
## [35] "dumpMethod" "dumpMethods"
## [37] "el" "el<-"
## [39] "elNamed" "elNamed<-"
## [41] "empty.dump" "emptyMethodsList"
## [43] "evalOnLoad" "evalqOnLoad"
## [45] "evalSource" "existsFunction"
## [47] "existsMethod" "extends"
## [49] "externalRefMethod" "finalDefaultMethod"
## [51] "findClass" "findFunction"
## [53] "findMethod" "findMethods"
## [55] "findMethodSignatures" "findUnique"
## [57] "fixPre1.8" "formalArgs"
## [59] "functionBody" "functionBody<-"
## [61] "generic.skeleton" "getAllSuperClasses"
## [63] "getClass" "getClassDef"
## [65] "getClasses" "getDataPart"
## [67] "getFunction" "getGeneric"
## [69] "getGenerics" "getGroup"
## [71] "getGroupMembers" "getLoadActions"
## [73] "getMethod" "getMethods"
## [75] "getMethodsForDispatch" "getMethodsMetaData"
## [77] "getPackageName" "getRefClass"
## [79] "getSlots" "getValidity"
## [81] "hasArg" "hasLoadAction"
## [83] "hasMethod" "hasMethods"
## [85] "implicitGeneric" "inheritedSlotNames"
## [87] "initFieldArgs" "initialize"
## [89] "initRefFields" "insertClassMethods"
## [91] "insertMethod" "insertSource"
## [93] "is" "isClass"
## [95] "isClassDef" "isClassUnion"
## [97] "isGeneric" "isGrammarSymbol"
## [99] "isGroup" "isRematched"
## [101] "isSealedClass" "isSealedMethod"
## [103] "isVirtualClass" "isXS3Class"
## [105] "kronecker" "languageEl"
## [107] "languageEl<-" "linearizeMlist"
## [109] "listFromMethods" "listFromMlist"
## [111] "loadMethod" "Logic"
## [113] "makeClassRepresentation" "makeExtends"
## [115] "makeGeneric" "makeMethodsList"
## [117] "makePrototypeFromClassDef" "makeStandardGeneric"
## [119] "matchSignature" "Math"
## [121] "Math2" "mergeMethods"
## [123] "metaNameUndo" "method.skeleton"
## [125] "MethodAddCoerce" "methodSignatureMatrix"
## [127] "MethodsList" "MethodsListSelect"
## [129] "methodsPackageMetaName" "missingArg"
## [131] "multipleClasses" "new"
## [133] "newBasic" "newClassRepresentation"
## [135] "newEmptyObject" "Ops"
## [137] "packageSlot" "packageSlot<-"
## [139] "possibleExtends" "prohibitGeneric"
## [141] "promptClass" "promptMethods"
## [143] "prototype" "Quote"
## [145] "rbind2" "reconcilePropertiesAndPrototype"
## [147] "registerImplicitGenerics" "rematchDefinition"
## [149] "removeClass" "removeGeneric"
## [151] "removeMethod" "removeMethods"
## [153] "representation" "requireMethods"
## [155] "resetClass" "resetGeneric"
## [157] "S3Class" "S3Class<-"
## [159] "S3Part" "S3Part<-"
## [161] "sealClass" "selectMethod"
## [163] "selectSuperClasses" "setAs"
## [165] "setClass" "setClassUnion"
## [167] "setDataPart" "setGeneric"
## [169] "setGenericImplicit" "setGroupGeneric"
## [171] "setIs" "setLoadAction"
## [173] "setLoadActions" "setMethod"
## [175] "setOldClass" "setPackageName"
## [177] "setPrimitiveMethods" "setRefClass"
## [179] "setReplaceMethod" "setValidity"
## [181] "show" "showClass"
## [183] "showDefault" "showExtends"
## [185] "showMethods" "showMlist"
## [187] "signature" "SignatureMethod"
## [189] "sigToEnv" "slot"
## [191] "slot<-" "slotNames"
## [193] "slotsFromS3" "substituteDirect"
## [195] "substituteFunctionArgs" "Summary"
## [197] "superClassDepth" "testInheritedMethods"
## [199] "testVirtual" "tryNew"
## [201] "unRematchDefinition" "validObject"
## [203] "validSlotNames"
## [1] 203
## <environment: namespace:utils>
## <environment: namespace:utils>
## <environment: namespace:base>
## <environment: namespace:grDevices>
## [1] "abline" "arrows" "assocplot" "axis"
## [5] "Axis" "axis.Date" "axis.POSIXct" "axTicks"
## [9] "barplot" "barplot.default" "box" "boxplot"
## [13] "boxplot.default" "boxplot.matrix" "bxp" "cdplot"
## [17] "clip" "close.screen" "co.intervals" "contour"
## [21] "contour.default" "coplot" "curve" "dotchart"
## [25] "erase.screen" "filled.contour" "fourfoldplot" "frame"
## [29] "grconvertX" "grconvertY" "grid" "hist"
## [33] "hist.default" "identify" "image" "image.default"
## [37] "layout" "layout.show" "lcm" "legend"
## [41] "lines" "lines.default" "locator" "matlines"
## [45] "matplot" "matpoints" "mosaicplot" "mtext"
## [49] "pairs" "pairs.default" "panel.smooth" "par"
## [53] "persp" "pie" "plot" "plot.default"
## [57] "plot.design" "plot.function" "plot.new" "plot.window"
## [61] "plot.xy" "points" "points.default" "polygon"
## [65] "polypath" "rasterImage" "rect" "rug"
## [69] "screen" "segments" "smoothScatter" "spineplot"
## [73] "split.screen" "stars" "stem" "strheight"
## [77] "stripchart" "strwidth" "sunflowerplot" "symbols"
## [81] "text" "text.default" "title" "xinch"
## [85] "xspline" "xyinch" "yinch"
## [1] "smoothScatter"
## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [25] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [37] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [49] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [61] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
## [73] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [85] FALSE FALSE FALSE
# 9.2 Argument Matching
bar <- matrix(data = 1:6, nrow = 3, ncol = 2,
dimnames = list(c("A", "B", "C"), c("D", "E")))
bar## D E
## A 1 4
## B 2 5
## C 3 6
## D E F
## A 1 4 7
## B 2 5 8
## C 3 6 9
# 9.2.4 Mixed
bar<-matrix(1:9,3,3,dim=list(c("A","B","C"),c("D","E","F")))
# 9.2.5 Dot-Dot-Dot: Use of Ellipses
args(data.frame)## function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,
## fix.empty.names = TRUE, stringsAsFactors = FALSE)
## NULL
## function (x, y, ...)
## NULL
# Exercise 10.2a
mynum <- 3
if (mynum == 0) {
result <- "zero"
} else if (mynum == 1) {
result <- "one"
} else if (mynum == 2) {
result <- "two"
} else if (mynum == 3) {
result <- "three"
} else if (mynum == 4) {
result <- "four"
} else if (mynum == 5) {
result <- "five"
} else if (mynum == 6) {
result <- "six"
} else if (mynum == 7) {
result <- "seven"
} else if (mynum == 8) {
result <- "eight"
} else if (mynum == 9) {
result <- "nine"
} else {
result <- "Invalid number"
}
print(result)## [1] "three"
## NULL
mynum<-3
result<-switch(mynum,0,1,2,3,4,5,6,7,8,9)
#b
# I
lowdose<-12.5
meddose <- 25.3
highdose <- 58.1
doselevel <- factor(c("Low","High","High","High","Low","Med",
"Med"),levels=c("Low","Med","High"))
doselevel## [1] Low High High High Low Med Med
## Levels: Low Med High
if ("High" %in% doselevel){
if(lowdose>=10){
lowdose<-10
} else {
lowdose <-lowdose/2
}
if(meddose>=26){
meddose<-26
}
if(highdose<60){
highdose<-60
}else{
highdose<-highdose*1.5
}
dosage<-rep(lowdose, length(doselevel))
dosage[doselevel=="Med"]<-meddose
dosage[doselevel=="High"]<-highdose
} else{
doselevel <- factor(x=doselevel,
levels = c("Low","Med"),
labels=c("Small","Large"))
if(lowdose<15&&meddose<35){
lowdose<-lowdose*2
meddose<-meddose+highdose
}
dosage<-rep(lowdose,length(doselevel))
dosage[doselevel=="Large"]<-meddose
}
dosage## [1] 10.0 60.0 60.0 60.0 10.0 25.3 25.3
# II
Lowdose<-12.5
meddose <- 25.3
highdose <- 58.1
doselevel <- factor(c("Low","Low","Low","Med","Low","Med",
"Med"),levels=c("Low","Med","High"))
if ("High" %in% doselevel){
if(lowdose>=10){
lowdose<-10
} else {
lowdose <-lowdose/2
}
if(meddose>=26){
meddose<-26
}
if(highdose<60){
highdose<-60
}else{
highdose<-highdose*1.5
}
dosage<-rep(lowdose, length(doselevel))
dosage[doselevel=="Med"]<-meddose
dosage[doselevel=="High"]<-highdose
} else{
doselevel <- factor(x=doselevel,
levels = c("Low","Med"),
labels=c("Small","Large"))
if(lowdose<15&&meddose<35){
lowdose<-lowdose*2
meddose<-meddose+highdose
}
dosage<-rep(lowdose,length(doselevel))
dosage[doselevel=="Large"]<-meddose
}
dosage## [1] 20.0 20.0 20.0 83.4 20.0 83.4 83.4
## [1] Small Small Small Large Small Large Large
## Levels: Small Large
# III
lowdose <- 9
meddose <- 49
highdose <- 61
doselevel <- factor(c("Low","Med","Med"),
levels=c("Low","Med","High"))
if ("High" %in% doselevel){
if(lowdose>=10){
lowdose<-10
} else {
lowdose <-lowdose/2
}
if(meddose>=26){
meddose<-26
}
if(highdose<60){
highdose<-60
}else{
highdose<-highdose*1.5
}
dosage<-rep(lowdose, length(doselevel))
dosage[doselevel=="Med"]<-meddose
dosage[doselevel=="High"]<-highdose
} else{
doselevel <- factor(x=doselevel,
levels = c("Low","Med"),
labels=c("Small","Large"))
if(lowdose<15&&meddose<35){
lowdose<-lowdose*2
meddose<-meddose+highdose
}
dosage<-rep(lowdose,length(doselevel))
dosage[doselevel=="Large"]<-meddose
}
dosage## [1] 9 49 49
## [1] Small Large Large
## Levels: Small Large
#iv
lowdose <- 9
meddose <- 49
highdose <- 61
doselevel <- factor(c("Low","High","High","High","Low","Med",
"Med"),levels=c("Low","Med","High"))
doselevel## [1] Low High High High Low Med Med
## Levels: Low Med High
if ("High" %in% doselevel){
if(lowdose>=10){
lowdose<-10
} else {
lowdose <-lowdose/2
}
if(meddose>=26){
meddose<-26
}
if(highdose<60){
highdose<-60
}else{
highdose<-highdose*1.5
}
dosage<-rep(lowdose, length(doselevel))
dosage[doselevel=="Med"]<-meddose
dosage[doselevel=="High"]<-highdose
} else{
doselevel <- factor(x=doselevel,
levels = c("Low","Med"),
labels=c("Small","Large"))
if(lowdose<15&&meddose<35){
lowdose<-lowdose*2
meddose<-meddose+highdose
}
dosage<-rep(lowdose,length(doselevel))
dosage[doselevel=="Large"]<-meddose
}
dosage## [1] 4.5 91.5 91.5 91.5 4.5 26.0 26.0
#c
mynum<-3
result<-switch(mynum,0,1,2,3,4,5)
mynum<-0
result<-switch(mynum,0,1,2,3,4,5)
mynum <- 3 # Try with other values like 0, 9, etc.
result <- ifelse(mynum >= 0 & mynum <= 9,
switch(mynum + 1, # switch is 1-indexed
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"),
"Invalid number")
print(result)## [1] "three"
mynum<-0
result <- ifelse(mynum >= 0 & mynum <= 9,
switch(mynum + 1, # switch is 1-indexed
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"),
"Invalid number")
# 10.2 Coding Loops
for(myitem in 5:7){
cat("--BRACED AREA BEGINS--\n")
cat("the current item is",myitem,"\n")
cat("--BRACED AREA ENDS--\n\n")
}## --BRACED AREA BEGINS--
## the current item is 5
## --BRACED AREA ENDS--
##
## --BRACED AREA BEGINS--
## the current item is 6
## --BRACED AREA ENDS--
##
## --BRACED AREA BEGINS--
## the current item is 7
## --BRACED AREA ENDS--
counter<-0
for(myitem in 5:7){
counter <- counter+1
cat("The item in run",counter,"is",myitem,"\n")
}## The item in run 1 is 5
## The item in run 2 is 6
## The item in run 3 is 7
mylist<-list(1:3, "hello", matrix(1:4, 2, 2))
for (i in 1:length(mylist)) {
item<-mylist[[1]]
print(paste("Item",i,"is of type",class(item)))
}## [1] "Item 1 is of type integer"
## [1] "Item 2 is of type integer"
## [1] "Item 3 is of type integer"
#You can also nest loops if one of the items is a matrix:
for(i in 1:length(mylist)){
item<-mylist[[1]]
if(is.matrix(item)){
for (r in 1:nrow(item)) {
for(c in 1:ncol(item)){
print(paste("Matrix element[",r,",",c,"]=",item[r,c]))
}
}
}
}
mylist## [[1]]
## [1] 1 2 3
##
## [[2]]
## [1] "hello"
##
## [[3]]
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
#Looping via Index
colors <- c("red", "green", "blue")
for (i in 1:length(colors)) {
print(paste("Color", i, "is", colors[i]))
}## [1] "Color 1 is red"
## [1] "Color 2 is green"
## [1] "Color 3 is blue"
## [1] "color 1 is red"
## [1] "color 2 is green"
## [1] "color 3 is blue"
## [1] 0.8
## [1] 2.2
## [1] 0.68
## [1] 1.1
## [1] 0.8
## [1] 2.2
## [1] 0.68
## [1] 1.1
foo <- list(aa=c(3.4,1),bb=matrix(1:4,2,2),cc=matrix(c(T,T,F,T,F,F),3,2),
dd="string here",ee=matrix(c("red","green","blue","yellow")))
foo## $aa
## [1] 3.4 1.0
##
## $bb
## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
##
## $cc
## [,1] [,2]
## [1,] TRUE TRUE
## [2,] TRUE FALSE
## [3,] FALSE FALSE
##
## $dd
## [1] "string here"
##
## $ee
## [,1]
## [1,] "red"
## [2,] "green"
## [3,] "blue"
## [4,] "yellow"
## [1] "aa" "bb" "cc" "dd" "ee"
## [1] NA NA NA NA NA
## [1] NA NA NA NA NA
for (i in 1:length(foo)) {
member<-foo[[i]]
if(is.matrix(member)){
is.mat[i]<-"Yes"
nr[i]<-nrow(member)
nc[i]<-ncol(member)
data.type[i]<-class(as.vector(member))
}else{
is.mat[i]<-'No'
}
}
bar<-data.frame(name,is.mat,nr,nc,data.type,stringsAsFactors =FALSE)
bar## name is.mat nr nc data.type
## 1 aa No NA NA <NA>
## 2 bb Yes 2 2 integer
## 3 cc Yes 3 2 logical
## 4 dd No NA NA <NA>
## 5 ee Yes 4 1 character
list_of_matrices<-list(mat1=matrix(1:4,2,2),mat2=matrix(5:8,2,2))
for (mat_name in names(list_of_matrices)) {
mat<-list_of_matrices[[mat_name]]
cat("Matrix:",mat_name,"\n")
for (i in 1:nrow(mat)) {
for (j in 1:ncol(mat)) {
cat("Element[",i,",",j,"]=",mat[i,j],"\n")
}
}
}## Matrix: mat1
## Element[ 1 , 1 ]= 1
## Element[ 1 , 2 ]= 3
## Element[ 2 , 1 ]= 2
## Element[ 2 , 2 ]= 4
## Matrix: mat2
## Element[ 1 , 1 ]= 5
## Element[ 1 , 2 ]= 7
## Element[ 2 , 1 ]= 6
## Element[ 2 , 2 ]= 8
# 10.3.2 The repeat Statement
fib.a<-1
fib.b<-1
repeat{
temp<-fib.a+fib.b
fib.a<-fib.b
fib.b<-temp
cat(fib.b,",",sep = "")
if(fib.b>150){
cat("BREAK NOW...\n")
break
}
}## 2,3,5,8,13,21,34,55,89,144,233,BREAK NOW...
#11.1.2 Using return
dummy1<-function(){
aa<-2.5
bb<-"string me along"
cc<-"string 'em up"
dd<-4:8
}
dummy2<-function(){
aa<-2.5
bb<-"string me along"
cc<-"string 'em up"
dd<-4:8
return (dd)
}
foo<-dummy1()
foo## [1] 4 5 6 7 8
## [1] 4 5 6 7 8
dummy3<-function(){
aa<-2.5
bb<-"string me along"
return(aa)
cc<-"string em up"
dd<-4:8
return(bb)
}
baz<-dummy3()
baz## [1] 2.5
# Exercise 11.1
#(a)
myfib4 <- function(thresh,printme){
if(printme){
fib.a <- 1
fib.b <- 1
cat(fib.a,", ",fib.b,", ",sep="")
repeat{
temp <- fib.a+fib.b
fib.a <- fib.b
fib.b <- temp
cat(fib.b,", ",sep="")
if(fib.b>thresh){
cat("BREAK NOW...")
break
}
}
} else {
fibseq <- c(1,1)
counter <- 2
repeat{
fibseq <- c(fibseq,fibseq[counter-1]+fibseq[counter])
counter <- counter+1
if(fibseq[counter]>thresh){
break
}
}
return(fibseq)
}
}
myfib4(thresh=150,printme=TRUE)## 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, BREAK NOW...
## 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, BREAK NOW...
## [1] 1 1 2 3 5 8 13 21 34 55 89 144 233
## [1] 1 1 2 3 5 8 13 21 34
## [10] 55 89 144 233 377 610 987 1597 2584
## [19] 4181 6765 10946 17711 28657 46368 75025 121393 196418
## [28] 317811 514229 832040 1346269
#b
mynum_factorial<-1
while(mynum>1){
mynum_factorial<-mynum_factorial*mynum
mynum<-mynum-1
}
mynum_factorial## [1] 1
myfac<-function(int){
result<-1
while(int>1){
result<-result*int
int<-int-1
}
return(result)
}
myfac(5)## [1] 120
## [1] 6.689503e+198
## [1] 1
##(ii)
myfac2 <- function(int){
if(int<0){
return(NaN)
}
result <- 1
while(int>1){
result <- result*int
int <- int-1
}
return(result)
}
myfac2(5)## [1] 120
## [1] 479001600
## [1] 1
## [1] NaN
#alternative
myfacii<-function(int){
if(int<0){
return(NaN)
}
result<-1
counter<-int
while (counter>1) {
result<-result*counter
counter<-counter-1
}
return(result)
}
myfac2(5) ## [1] 120
## [1] 479001600
## [1] 1
## [1] NaN
## [1] "This is a loop number 1"
## [1] "This is a loop number 2"
## [1] "This is a loop number 3"
## [1] "This is a loop number 4"
## [1] "This is a loop number 5"
a<-1
b<-3
add_numbers<-function(a,b){
result<-a+b
return(result)
}
myfacm<-function(int){
result<-1
while(int>1){
result<-result*int
int<-int-1
}
result(result)
}
c_to_f<-function(celsius){
return((celsius*9/5)+32)
}# Exercise 11.2
Compound_F<-function(P,i,y,t=12,ploitit=TRUE,...){
times<-1:y
F<-P*(1+i/(100*t))^(t*times)
if (ploitit){
plot(times,F,type="S",xlab="Year (y)",ylab="Balanced(F)",main="Compound Interest Calculator", ...)
}else{
return(F)
}
}
#11.2i
Compound_F(P=5000,i=4.4,y=10,ploitit = FALSE)## [1] 5224.491 5459.062 5704.164 5960.271 6227.877 6507.498 6799.674 7104.967
## [9] 7423.968 7757.291
#11.2ii
Compound_F(P=100,i=22.9,y=20,ploitit=TRUE)
#11.2iii
Compound_F(P=100,i=22.9,y=20,ploitit=TRUE)
annualF<-Compound_F(P=100,i=22.9,y=20,ploitit = FALSE)
lines(1:20,annualF,type = "s",col="Pink",lty=3)
legend("topleft",legend = c('Monthly Interest',"Annually Interest"),col=c("blue","black"),lty=c(1,2))#11.2B
Equation<-function(k1,k2,k3){
if(missing(k1)||missing(k2)||missing(k3)){
return("One or more of the arguments k1, k2, or k3 is missing")
}
D<-k2^2-4*k1 * k3
if(D<0){
cat("no real roots.")
return(numeric(0))
}else if (D==0){
return(-k2/(2*k1))
}else{
return(c((-k2-D^0.5)/(2*k1),(-k2+D^0.5)/(2*k1)))
}
}
##(i)
Equation(k1=2,k2=-1,k3=-5)## [1] -1.350781 1.850781
## no real roots.
## numeric(0)
## [1] -0.3691106 6.5229567
## [1] 0.6666667
## [1] -1.278312 2.849740
## no real roots.
## numeric(0)
## [1] "One or more of the arguments k1, k2, or k3 is missing"
## 11.3 Specialized Functions
# Externally Defined
multiples_helper_ext <- function(x,matrix.flags,mat){
indexes <- which(matrix.flags)
counter <- 0
result <- list()
for(i in indexes){
temp <- x[[i]]
if(ncol(temp)==nrow(mat)){
counter <- counter+1
result[[counter]] <- temp%*%mat
}
}
return(list(result,counter))
}
multiples4 <- function(x,mat,str1="no valid matrices",str2=str1){
matrix.flags <- sapply(x,FUN=is.matrix)
if(!any(matrix.flags)){
return(str1)
}
helper.call <- multiples_helper_ext(x,matrix.flags,mat)
result <- helper.call[[1]]
counter <- helper.call[[2]]
if(counter==0){
return(str2)
} else {
return(result)
}
}#Part C: Geometric Mean Function for a List
#Create a function geolist() that:
# Loops through a list.
#If the item is a vector → compute geometric mean.
#If it’s a matrix → compute geometric mean for each row.
geolist <- function(lst) {
geo_mean <- function(x) {
prod(x)^(1 / length(x))
}
for (i in seq_along(lst)) {
if (is.vector(lst[[i]])) {
lst[[i]] <- geo_mean(lst[[i]])
} else if (is.matrix(lst[[i]])) {
lst[[i]] <- apply(lst[[i]], 1, geo_mean)
}
}
return(lst)
}
foo <- list(
1:3,
matrix(c(3.3, 3.2, 2.8, 2.1, 4.6, 4.5, 3.1, 9.4), 4, 2),
matrix(c(3.3, 3.2, 2.8, 2.1, 4.6, 4.5, 3.1, 9.4), 2, 4)
)
geolist(foo)## [[1]]
## [1] 1.817121
##
## [[2]]
## [1] 3.896152 3.794733 2.946184 4.442972
##
## [[3]]
## [1] 3.388035 4.106080
## [[1]]
## [1] 4.147166
##
## [[2]]
## [1] 4.147166
##
## [[3]]
## [1] 1 2 3 4 5 6 7 8 9
##
## [[4]]
## [1] 3.036589 4.308869 5.451362