Part 2

Exercise 2.1

  1. Create a vector called myNumbers with 201 values ranging from 100 to 300 hint use c()
myNumbers <- c(100:300)
myNumbers
##   [1] 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
##  [19] 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
##  [37] 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
##  [55] 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
##  [73] 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
##  [91] 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
## [109] 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
## [127] 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
## [145] 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
## [163] 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
## [181] 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
## [199] 298 299 300
  1. Look at the first few and the last few numbers of your new vector hint use head() and tail().
head(myNumbers)
## [1] 100 101 102 103 104 105
tail(myNumbers)
## [1] 295 296 297 298 299 300
  1. Calculate the mean of your vector hint use mean()
mean(myNumbers)
## [1] 200

Exercise 2.2

  1. Find the function to calculate standard deviation and median hint use ??

  2. Create a variable called m_func_var that stores all the functions and variables on your system that start with the letter m.

m_func_var <- apropos("^m")

m_func_var
##   [1] "mad"                       "mahalanobis"              
##   [3] "maintainer"                "make_translations_pkg"    
##   [5] "make.link"                 "make.names"               
##   [7] "make.packages.html"        "make.rgb"                 
##   [9] "make.socket"               "make.unique"              
##  [11] "makeActiveBinding"         "makeARIMA"                
##  [13] "makeClassRepresentation"   "makeExtends"              
##  [15] "makeGeneric"               "makeMethodsList"          
##  [17] "makepredictcall"           "makePrototypeFromClassDef"
##  [19] "makeRweaveLatexCodeRunner" "makeStandardGeneric"      
##  [21] "makevars_site"             "makevars_user"            
##  [23] "manova"                    "mantelhaen.test"          
##  [25] "Map"                       "mapply"                   
##  [27] "margin.table"              "marginSums"               
##  [29] "mat.or.vec"                "match"                    
##  [31] "match.arg"                 "match.call"               
##  [33] "match.fun"                 "matchSignature"           
##  [35] "Math"                      "Math.data.frame"          
##  [37] "Math.Date"                 "Math.difftime"            
##  [39] "Math.factor"               "Math.POSIXt"              
##  [41] "Math2"                     "matlines"                 
##  [43] "matplot"                   "matpoints"                
##  [45] "matrix"                    "mauchly.test"             
##  [47] "max"                       "max.col"                  
##  [49] "mcnemar.test"              "md5sum"                   
##  [51] "mdeaths"                   "mean"                     
##  [53] "mean.Date"                 "mean.default"             
##  [55] "mean.difftime"             "mean.POSIXct"             
##  [57] "mean.POSIXlt"              "median"                   
##  [59] "median.default"            "medpolish"                
##  [61] "mem.maxNSize"              "mem.maxVSize"             
##  [63] "memCompress"               "memDecompress"            
##  [65] "memory.limit"              "memory.profile"           
##  [67] "memory.size"               "menu"                     
##  [69] "merge"                     "merge.data.frame"         
##  [71] "merge.default"             "mergeMethods"             
##  [73] "message"                   "metaNameUndo"             
##  [75] "method.skeleton"           "MethodAddCoerce"          
##  [77] "methods"                   "methodSignatureMatrix"    
##  [79] "MethodsList"               "MethodsListSelect"        
##  [81] "methodsPackageMetaName"    "mget"                     
##  [83] "min"                       "mirror2html"              
##  [85] "missing"                   "missingArg"               
##  [87] "Mod"                       "mode"                     
##  [89] "mode<-"                    "model.extract"            
##  [91] "model.frame"               "model.frame.default"      
##  [93] "model.matrix"              "model.matrix.default"     
##  [95] "model.matrix.lm"           "model.offset"             
##  [97] "model.response"            "model.tables"             
##  [99] "model.weights"             "modifyList"               
## [101] "month.abb"                 "month.name"               
## [103] "monthplot"                 "months"                   
## [105] "months.Date"               "months.POSIXt"            
## [107] "mood.test"                 "morley"                   
## [109] "mosaicplot"                "mostattributes<-"         
## [111] "mtcars"                    "mtext"                    
## [113] "multipleClasses"           "mvfft"                    
## [115] "my.at"                     "myFirstVector"            
## [117] "myNumbers"
  1. By default, head() and tail() list the first or last 6 data points of whatever is passed to them. Use the help page for head() and tail() to list the first and last 10 results of m_func_var.
head(m_func_var, n=10)
##  [1] "mad"                   "mahalanobis"           "maintainer"           
##  [4] "make_translations_pkg" "make.link"             "make.names"           
##  [7] "make.packages.html"    "make.rgb"              "make.socket"          
## [10] "make.unique"
tail(m_func_var, n=10)
##  [1] "morley"           "mosaicplot"       "mostattributes<-" "mtcars"          
##  [5] "mtext"            "multipleClasses"  "mvfft"            "my.at"           
##  [9] "myFirstVector"    "myNumbers"
  1. Calculate the standard deviation and median of your vector hint use sd() and median()
sd(x)
## [1] 15.81139
median(x)
## [1] 55

Part 3

Exercise 3.1

Find all the as. functions available in R. Note that a . is a special character in a regular expression search, so to search for the . we need to comment it out to override itโ€™s 'specialness'. We do so with a \. However, \ is also a special character, so we need to comment it out too, with an additional \.

apropos("^as\\.")
##   [1] "as.array"                      "as.array.default"             
##   [3] "as.call"                       "as.character"                 
##   [5] "as.character.condition"        "as.character.Date"            
##   [7] "as.character.default"          "as.character.error"           
##   [9] "as.character.factor"           "as.character.hexmode"         
##  [11] "as.character.numeric_version"  "as.character.octmode"         
##  [13] "as.character.POSIXt"           "as.character.srcref"          
##  [15] "as.complex"                    "as.data.frame"                
##  [17] "as.data.frame.array"           "as.data.frame.AsIs"           
##  [19] "as.data.frame.character"       "as.data.frame.complex"        
##  [21] "as.data.frame.data.frame"      "as.data.frame.Date"           
##  [23] "as.data.frame.default"         "as.data.frame.difftime"       
##  [25] "as.data.frame.factor"          "as.data.frame.integer"        
##  [27] "as.data.frame.list"            "as.data.frame.logical"        
##  [29] "as.data.frame.matrix"          "as.data.frame.model.matrix"   
##  [31] "as.data.frame.noquote"         "as.data.frame.numeric"        
##  [33] "as.data.frame.numeric_version" "as.data.frame.ordered"        
##  [35] "as.data.frame.POSIXct"         "as.data.frame.POSIXlt"        
##  [37] "as.data.frame.raw"             "as.data.frame.table"          
##  [39] "as.data.frame.ts"              "as.data.frame.vector"         
##  [41] "as.Date"                       "as.Date.character"            
##  [43] "as.Date.default"               "as.Date.factor"               
##  [45] "as.Date.numeric"               "as.Date.POSIXct"              
##  [47] "as.Date.POSIXlt"               "as.dendrogram"                
##  [49] "as.difftime"                   "as.dist"                      
##  [51] "as.double"                     "as.double.difftime"           
##  [53] "as.double.POSIXlt"             "as.environment"               
##  [55] "as.expression"                 "as.expression.default"        
##  [57] "as.factor"                     "as.formula"                   
##  [59] "as.function"                   "as.function.default"          
##  [61] "as.graphicsAnnot"              "as.hclust"                    
##  [63] "as.hexmode"                    "as.integer"                   
##  [65] "as.list"                       "as.list.data.frame"           
##  [67] "as.list.Date"                  "as.list.default"              
##  [69] "as.list.difftime"              "as.list.environment"          
##  [71] "as.list.factor"                "as.list.function"             
##  [73] "as.list.numeric_version"       "as.list.POSIXct"              
##  [75] "as.list.POSIXlt"               "as.logical"                   
##  [77] "as.logical.factor"             "as.matrix"                    
##  [79] "as.matrix.data.frame"          "as.matrix.default"            
##  [81] "as.matrix.noquote"             "as.matrix.POSIXlt"            
##  [83] "as.name"                       "as.null"                      
##  [85] "as.null.default"               "as.numeric"                   
##  [87] "as.numeric_version"            "as.octmode"                   
##  [89] "as.ordered"                    "as.package_version"           
##  [91] "as.pairlist"                   "as.person"                    
##  [93] "as.personList"                 "as.POSIXct"                   
##  [95] "as.POSIXct.Date"               "as.POSIXct.default"           
##  [97] "as.POSIXct.numeric"            "as.POSIXct.POSIXlt"           
##  [99] "as.POSIXlt"                    "as.POSIXlt.character"         
## [101] "as.POSIXlt.Date"               "as.POSIXlt.default"           
## [103] "as.POSIXlt.factor"             "as.POSIXlt.numeric"           
## [105] "as.POSIXlt.POSIXct"            "as.qr"                        
## [107] "as.raster"                     "as.raw"                       
## [109] "as.relistable"                 "as.roman"                     
## [111] "as.single"                     "as.single.default"            
## [113] "as.stepfun"                    "as.symbol"                    
## [115] "as.table"                      "as.table.default"             
## [117] "as.ts"                         "as.vector"                    
## [119] "as.vector.factor"

Exercise 3.3

  1. How many of the values in dailyHighs are higher than 27:
length(dailyHighs[dailyHighs > 27])
## [1] 8
  1. Get a summary, similar to this, of the data in Temperature.

Note your values will be different, but the descriptive categories should be the same.

summary(dailyHighs)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   20.00   21.75   25.50   26.05   31.00   34.00

Exercise 3.4

Replace the temperatures in Temperature that are above 27, but this time, replace them with values that are double their own, so if you have 30, it becomes 60, 32, 64 and so on.

dailyHighsMultiplied <- replace(dailyHighs, dailyHighs > 27, dailyHighs[dailyHighs > 27] * 2) ## Vector, condition, values where this condition is true * n

dailyHighsMultiplied
##  [1] 24 22 56 25 68 26 23 21 68 20 21 20 26 62 64 62 60 20 62 22

Part 4

Exercise 4.1

Take a few minutes and try to:

  • sort your Height vector data from lowest to highest value;
  • list only the unique values in this same data;
  • list a summary of your data as if it were categorical data;
  • sort your summary from above from lowest to highest.

Do the above using the following functions:

  • sort()
  • unique()
  • as.factor
  • summary
sort(Height)
##  [1] 121 130 136 139 142 153 154 155 157 158 161 163 163 166 168 176 180 184 186
## [20] 190
sort(unique(Height))
##  [1] 121 130 136 139 142 153 154 155 157 158 161 163 166 168 176 180 184 186 190
sort(summary(as.factor(Height)))
## 121 130 136 139 142 153 154 155 157 158 161 166 168 176 180 184 186 190 163 
##   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   2

Sort our matrix, by Height and then by Weight. Do this using the function order()

Note

sort() rearranges a vector in ascending or descending order while order() organizes a multi-dimensional object based on individual vectors.

completePhysDataSet[ order(completePhysDataSet[, "Height"], completePhysDataSet[, "Weight"]), ]
##       Height Weight Age
##  [1,]    120     67  21
##  [2,]    121     89  22
##  [3,]    123     55  21
##  [4,]    127     54  20
##  [5,]    130     75  24
##  [6,]    131     65  24
##  [7,]    132     55  21
##  [8,]    136     73  23
##  [9,]    139     60  21
## [10,]    142     79  23
## [11,]    147     61  21
## [12,]    149     83  21
## [13,]    151     83  22
## [14,]    153     76  23
## [15,]    153     88  20
## [16,]    154     65  22
## [17,]    155     59  21
## [18,]    155     62  21
## [19,]    156     76  22
## [20,]    157     55  20
## [21,]    158     54  22
## [22,]    159     69  22
## [23,]    161     69  20
## [24,]    163     66  23
## [25,]    163     70  23
## [26,]    166     63  25
## [27,]    168     51  21
## [28,]    170     83  20
## [29,]    171     67  22
## [30,]    174     83  25
## [31,]    176     61  22
## [32,]    178     57  23
## [33,]    180     72  22
## [34,]    184     60  23
## [35,]    184     66  24
## [36,]    184     80  22
## [37,]    185     62  21
## [38,]    186     83  24
## [39,]    190     60  20
## [40,]    190     63  20