9.1.1 Environments

# Package Environments and Namespaces
ls("package:graphics")
##  [1] "abline"          "arrows"          "assocplot"      
##  [4] "axis"            "Axis"            "axis.Date"      
##  [7] "axis.POSIXct"    "axTicks"         "barplot"        
## [10] "barplot.default" "box"             "boxplot"        
## [13] "boxplot.default" "boxplot.matrix"  "bxp"            
## [16] "cdplot"          "clip"            "close.screen"   
## [19] "co.intervals"    "contour"         "contour.default"
## [22] "coplot"          "curve"           "dotchart"       
## [25] "erase.screen"    "filled.contour"  "fourfoldplot"   
## [28] "frame"           "grconvertX"      "grconvertY"     
## [31] "grid"            "hist"            "hist.default"   
## [34] "identify"        "image"           "image.default"  
## [37] "layout"          "layout.show"     "lcm"            
## [40] "legend"          "lines"           "lines.default"  
## [43] "locator"         "matlines"        "matplot"        
## [46] "matpoints"       "mosaicplot"      "mtext"          
## [49] "pairs"           "pairs.default"   "panel.smooth"   
## [52] "par"             "persp"           "pie"            
## [55] "plot"            "plot.default"    "plot.design"    
## [58] "plot.function"   "plot.new"        "plot.window"    
## [61] "plot.xy"         "points"          "points.default" 
## [64] "polygon"         "polypath"        "rasterImage"    
## [67] "rect"            "rug"             "screen"         
## [70] "segments"        "smoothScatter"   "spineplot"      
## [73] "split.screen"    "stars"           "stem"           
## [76] "strheight"       "stripchart"      "strwidth"       
## [79] "sunflowerplot"   "symbols"         "text"           
## [82] "text.default"    "title"           "xinch"          
## [85] "xspline"         "xyinch"          "yinch"

9.1.2 Search Path

# Viewing R's search path
search()
## [1] ".GlobalEnv"        "package:stats"     "package:graphics" 
## [4] "package:grDevices" "package:utils"     "package:datasets" 
## [7] "package:methods"   "Autoloads"         "package:base"
# Enclosing environment
environment(seq)
## <environment: namespace:base>
# Updated package found via search()
library(car)
## Loading required package: carData
search()
##  [1] ".GlobalEnv"        "package:car"       "package:carData"  
##  [4] "package:stats"     "package:graphics"  "package:grDevices"
##  [7] "package:utils"     "package:datasets"  "package:methods"  
## [10] "Autoloads"         "package:base"
# List all objects in global environment
ls()
## character(0)
# Remove all objects in global environment
rm()

Exercise 9.1

# a.
ls("package:methods")[1:20]
##  [1] "addNextMethod"         "allGenerics"          
##  [3] "allNames"              "Arith"                
##  [5] "as"                    "as<-"                 
##  [7] "asMethodDefinition"    "assignClassDef"       
##  [9] "assignMethodsMetaData" "balanceMethodsList"   
## [11] "body<-"                "cacheGenericsMetaData"
## [13] "cacheMetaData"         "cacheMethod"          
## [15] "callGeneric"           "callNextMethod"       
## [17] "canCoerce"             "cbind2"               
## [19] "checkAtAssignment"     "checkSlotAssignment"
# a.
length(ls("package:methods"))
## [1] 218
# b i.
environment(read.table)
## <environment: namespace:utils>
# b ii.
environment(data)
## <environment: namespace:utils>
# b iii.
environment(matrix)
## <environment: namespace:base>
# b iv.
environment(jpeg)
## <environment: namespace:grDevices>
any(ls("package:graphics")=="smoothScatter")
## [1] TRUE

9.2.5 Dot-Dot-Dot: Use of Ellipses

# Functions with unknown number of args.
args(data.frame)
## function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, 
##     fix.empty.names = TRUE, stringsAsFactors = default.stringsAsFactors()) 
## NULL

Exercise 9.2

# a.
seq(-4,4, 0.2)
##  [1] -4.0 -3.8 -3.6 -3.4 -3.2 -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4
## [15] -1.2 -1.0 -0.8 -0.6 -0.4 -0.2  0.0  0.2  0.4  0.6  0.8  1.0  1.2  1.4
## [29]  1.6  1.8  2.0  2.2  2.4  2.6  2.8  3.0  3.2  3.4  3.6  3.8  4.0
# b i.
array(8:1, dim=c(2,2,2))
## , , 1
## 
##      [,1] [,2]
## [1,]    8    6
## [2,]    7    5
## 
## , , 2
## 
##      [,1] [,2]
## [1,]    4    2
## [2,]    3    1
# Mixed data positional and dim exact.
# b ii.
rep(1:2,3)
## [1] 1 2 1 2 1 2
# positional
# b iii.
seq(from = 10, to = 8, length = 5)
## [1] 10.0  9.5  9.0  8.5  8.0
# mixed from and to exact and length.out partial
sort(decreasing = T, x = c(2,1,1,2,0.3,3,1.3))
## [1] 3.0 2.0 2.0 1.3 1.0 1.0 0.3
# Exact
# b iv.
which(matrix(c(T,F,T,T),2,2)) 
## [1] 1 3 4
# Positional
# b vi.
which(matrix(c(T,F,T,T),2,2),a=T) 
##      row col
## [1,]   1   1
## [2,]   1   2
## [3,]   2   2
# Mixed: 'x', 'data', 'nrow', 'ncol' positional, 'arr.ind' partial
#(c)
# 'pch', 'lwd', 'lty' and 'col' are part of the ellipsis.