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] 79.40777 93.67965 64.48942 83.92610 62.76801 72.33601 71.23298
## [8] 85.95712 73.72947 78.65953 73.53396 81.65373 58.11868 77.65261
## [15] 72.78406 67.83484 91.68256 87.74868 99.12162 74.13258 84.18518
## [22] 70.27978 92.05829 80.82594 57.16043 60.10216 75.56725 78.22666
## [29] 84.81183 55.71056 78.55313 82.15982 97.69633 75.19532 73.66613
## [36] 55.18405 90.17013 82.48410 77.25069 80.65793 67.82123 91.04821
## [43] 78.88593 61.09947 75.72507 55.09005 79.53213 78.25726 77.30094
## [50] 79.22970 64.61481 74.71267 68.56241 76.92380 71.86919 66.43743
## [57] 73.54383 79.76383 71.32687 68.83973 73.58706 95.65770 64.79270
## [64] 77.41132 79.23114 67.24428 72.28655 87.92083 64.27759 67.44583
## [71] 79.27735 55.52705 65.78327 72.99402 74.91909 84.26697 73.95872
## [78] 70.95268 60.86386 53.31450 67.06291 87.99420 73.00118 100.76648
## [85] 66.82406 69.21703 85.16120 92.09102 80.71345 53.15071 82.90487
## [92] 83.44290 73.97631 67.72106 87.97380 61.11711 81.74395 87.02317
## [99] 68.17240 98.11290 89.40778 88.46569 73.52503 82.90093 89.53422
## [106] 67.05143 81.55551 66.70536 75.55350 86.63349 64.18960 74.38655
## [113] 55.79734 72.74530 65.45245 74.21972 85.87767 70.25497 98.93257
## [120] 83.94439 98.11615 88.22746 73.98209 82.30826 80.68239 55.66000
## [127] 85.17957 77.13492 88.40194 71.74634 83.14989 95.39438 70.24548
## [134] 66.92362 75.92157 78.90076 77.52614 71.43340 92.31019 62.76088
## [141] 57.46697 80.34018 71.31450 63.92295 84.02385 83.63841 75.29952
## [148] 77.55866 77.69876 60.84508 76.46578 84.85734 85.63429 74.53200
## [155] 72.01993 69.92896 93.10340 79.55123 86.40205 80.89982 76.91374
## [162] 69.29695 85.39565 79.53833 80.79954 70.25807 72.09044 76.38144
## [169] 75.82797 58.06997 86.25243 69.85701 92.77243 77.57743 71.48591
## [176] 79.22354 76.53823 60.41786 67.45135 62.27740 76.25881 78.27528
## [183] 64.49060 78.85269 69.46111 86.62161 57.74740 73.93426 63.47003
## [190] 93.79304 70.78235 66.70937 68.79268 73.03223 85.18748 51.93879
## [197] 81.75481 71.68646 63.40160 80.56073sd(curvedgrades)## [1] 10.42994min(curvedgrades)## [1] 51.93879max(curvedgrades)## [1] 100.7665mean(curvedgrades)## [1] 75.78341hist(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("https://stats.are-awesome.com/datasets/gorilla_sizes.txt", header=TRUE)
#figure out sex
## do it with grepl
isMale <- factor(grepl("m", gorilla$specimen))
#rename the trues and falses by updating the labels for each factor level
levels(isMale) <- c("female", "male")
gorilla$sex <- isMale
#alternate way - first make a new column which will keep track of sex, with 59 NA values as a placeholder
gorilla$sex <- rep(NA, 59)
# now 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 29hist(log(gorilla$skull))
smalls <- gorilla[gorilla$skull < 250,]
smalls## specimen skull sex
## 1 _0_f1 235.180 Female
## 2 _0_f2 238.968 Female
## 3 _0_f3 235.168 Female
## 4 _0_f4 242.873 Female
## 5 _0_f5 229.244 Female
## 6 _0_f6 240.477 Female
## 7 _0_f7 242.761 Female
## 8 _0_f8 240.322 Female
## 9 _0_f9 237.948 Female
## 10 _0_f10 224.605 Female
## 11 _0_f11 243.091 Female
## 12 _0_f12 240.709 Female
## 13 _0_f13 241.155 Female
## 14 _0_f14 231.046 Female
## 15 _0_f15 228.324 Female
## 16 _0_f16 228.183 Female
## 17 _0_f17 246.153 Female
## 18 _0_f18 228.614 Female
## 19 _0_f19 236.424 Female
## 20 _0_f20 243.491 Female
## 21 _0_f21 229.924 Female
## 22 _0_f22 236.198 Female
## 23 _0_f23 238.134 Female
## 24 _0_f24 238.700 Female
## 25 _0_f25 244.689 Female
## 26 _0_f26 227.045 Female
## 27 _0_f27 232.538 Female
## 28 _0_f28 245.203 Female
## 29 _0_f29 240.910 Female
## 30 _0_f30 245.259 Femalebigs <- 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