1. Indexing Vectors

5 points possible

  • 1pt length
  • 1pt first four
  • 1pt last 8
  • 1pt various
  • 1pt reverse
nouns <- c("apple", "flower", "insect", "lettuce", "knife", "dog", "cloud", "person", "cabinet", "flower" )

#length
length(nouns)
## [1] 10
#first four
nouns[1:4]
## [1] "apple"   "flower"  "insect"  "lettuce"
#last 8
nouns[3:10]
## [1] "insect"  "lettuce" "knife"   "dog"     "cloud"   "person"  "cabinet"
## [8] "flower"
#various elements
nouns[c(1, 3:6, 10)]
## [1] "apple"   "insect"  "lettuce" "knife"   "dog"     "flower"
#reverse
nouns[10:1]
##  [1] "flower"  "cabinet" "person"  "cloud"   "dog"     "knife"   "lettuce"
##  [8] "insect"  "flower"  "apple"
#alternate reverse
rev(nouns)
##  [1] "flower"  "cabinet" "person"  "cloud"   "dog"     "knife"   "lettuce"
##  [8] "insect"  "flower"  "apple"

2. Using functions

7 points possible

  • 1pt grades
  • 1pt curve
  • 1pt sd
  • 1pt min
  • 1pt max
  • 1pt mean
  • 1pt hist
grades <- rnorm(200, mean = 68, sd=10)

#curve
curvedgrades <- grades + 7
curvedgrades
##   [1] 81.96015 79.17397 82.02658 78.23057 72.27824 69.57594 80.08276
##   [8] 87.29229 91.55017 63.59312 73.30077 74.48292 63.89946 90.89385
##  [15] 72.04848 89.22473 72.01990 71.65147 74.98914 80.61545 72.49823
##  [22] 69.10758 78.19586 80.32436 87.17072 83.75444 81.11645 88.04294
##  [29] 90.65109 79.76003 84.83487 87.28325 66.38662 71.84786 95.50913
##  [36] 46.22264 73.48653 72.99583 71.40836 67.36490 92.21720 74.23947
##  [43] 63.86949 86.99961 72.52997 63.57775 67.53362 82.21618 70.36195
##  [50] 50.81155 98.21705 62.47593 62.60531 58.01816 77.11611 76.38442
##  [57] 59.85663 73.25545 61.51678 61.17567 80.91648 62.08179 78.49762
##  [64] 92.56509 72.36872 66.75120 65.79000 86.36979 74.80959 91.86876
##  [71] 87.88960 78.20243 85.36698 79.56468 86.38112 81.71750 68.71195
##  [78] 63.08636 44.40474 63.13618 60.67608 50.37694 70.16508 92.25895
##  [85] 75.13971 76.57234 76.69121 85.64577 71.66357 60.99881 84.37473
##  [92] 73.86891 76.63086 81.79394 76.70329 56.58130 74.91526 58.27463
##  [99] 73.57128 69.95557 73.32655 74.59835 68.44893 88.35940 56.54251
## [106] 76.77497 99.02658 73.64593 56.96252 53.57628 69.46739 87.58783
## [113] 69.45641 61.52510 73.40897 62.45474 87.78781 90.38289 66.10796
## [120] 87.69804 80.36288 83.91009 81.93828 68.20555 88.02365 68.00535
## [127] 78.75154 64.24395 55.11121 70.43838 65.41531 69.20611 91.84989
## [134] 68.99089 72.64446 72.30465 80.06736 63.68566 77.28214 72.98622
## [141] 95.96889 50.40129 78.29160 75.83495 81.12224 82.05407 66.32530
## [148] 84.78656 81.52934 81.16057 72.99623 87.46676 82.26448 70.15998
## [155] 69.30721 76.77148 80.52941 64.85980 84.11507 73.65416 63.83123
## [162] 46.47046 65.58044 87.78111 60.32214 83.58513 73.99351 77.90451
## [169] 75.57279 89.03322 77.70100 76.72638 78.81919 78.06610 87.46248
## [176] 92.38832 62.03222 60.04817 75.79424 45.95524 80.33554 69.81196
## [183] 68.21303 75.22736 70.12891 59.49230 94.11275 70.69650 79.78536
## [190] 75.01791 84.02572 58.95414 63.42813 58.94910 76.72353 62.49433
## [197] 87.54272 79.10354 81.03545 74.57523
sd(curvedgrades)
## [1] 10.95884
min(curvedgrades)
## [1] 44.40474
max(curvedgrades)
## [1] 99.02658
mean(curvedgrades)
## [1] 74.44026
hist(curvedgrades)

3. Organizing data

4 points possible

  • 2pts - fix data
  • 2pts - describe problems

The original data file had duplicated column names (length and diameter), relying on another column to differentiate the anatomical element. This is a no, no. Column names must stand alone (e.g. tibia_diameter).

The rows containing species names violate the 1 row = 1 observation priciple. These rows should be turned into a column which records the species for each observation.

4. Manipulation

7 pts possible

  • 1pt gorilla
  • 2pts sex
  • 1pt table
  • 1pt hist
  • 1pt small
  • 1pt big
gorilla <- read.table("http://hompal-stats.wabarr.com/datasets/gorilla_sizes.txt", header=TRUE)

#figure out sex

#make a new column for sex, with 59 NA values as a placeholder
gorilla$sex <- rep(NA, 59)

#replace the NA values with 'Male' or 'Female' with the help of grep()
gorilla$sex[grep("m", gorilla$specimen)] <- "Male"
gorilla$sex[grep("f", gorilla$specimen)] <- "Female"

table(gorilla$sex)
## 
## Female   Male 
##     30     29
hist(log(gorilla$skull))

smalls <- gorilla[gorilla$skull > 250,]
smalls
##    specimen   skull  sex
## 31    _1_m1 270.137 Male
## 32    _1_m2 273.371 Male
## 33    _1_m3 292.082 Male
## 34    _1_m4 276.194 Male
## 35    _1_m5 282.821 Male
## 36    _1_m6 261.352 Male
## 37    _1_m7 267.413 Male
## 38    _1_m8 286.614 Male
## 39    _1_m9 288.243 Male
## 40   _1_m10 287.369 Male
## 41   _1_m11 275.070 Male
## 42   _1_m12 302.074 Male
## 43   _1_m13 280.033 Male
## 44   _1_m14 276.853 Male
## 45   _1_m15 273.564 Male
## 46   _1_m16 287.930 Male
## 47   _1_m17 273.241 Male
## 48   _1_m18 271.774 Male
## 49   _1_m19 287.890 Male
## 50   _1_m20 296.964 Male
## 51   _1_m21 283.813 Male
## 52   _1_m22 279.941 Male
## 53   _1_m23 300.346 Male
## 54   _1_m24 291.276 Male
## 55   _1_m25 262.899 Male
## 56   _1_m26 275.778 Male
## 57   _1_m27 276.420 Male
## 58   _1_m28 280.518 Male
## 59   _1_m29 286.621 Male
bigs <- subset(gorilla, subset = skull>250)
bigs
##    specimen   skull  sex
## 31    _1_m1 270.137 Male
## 32    _1_m2 273.371 Male
## 33    _1_m3 292.082 Male
## 34    _1_m4 276.194 Male
## 35    _1_m5 282.821 Male
## 36    _1_m6 261.352 Male
## 37    _1_m7 267.413 Male
## 38    _1_m8 286.614 Male
## 39    _1_m9 288.243 Male
## 40   _1_m10 287.369 Male
## 41   _1_m11 275.070 Male
## 42   _1_m12 302.074 Male
## 43   _1_m13 280.033 Male
## 44   _1_m14 276.853 Male
## 45   _1_m15 273.564 Male
## 46   _1_m16 287.930 Male
## 47   _1_m17 273.241 Male
## 48   _1_m18 271.774 Male
## 49   _1_m19 287.890 Male
## 50   _1_m20 296.964 Male
## 51   _1_m21 283.813 Male
## 52   _1_m22 279.941 Male
## 53   _1_m23 300.346 Male
## 54   _1_m24 291.276 Male
## 55   _1_m25 262.899 Male
## 56   _1_m26 275.778 Male
## 57   _1_m27 276.420 Male
## 58   _1_m28 280.518 Male
## 59   _1_m29 286.621 Male