Instructor's Manual for Exploratory Data Analysis Using R Ronald K. Pearson 1 / 4
Part I Analyzing Data Interactively with R
1 2 / 4
Chapter 1 Data, Exploratory Analysis, and R
Exercise 1: Section 1.2.2 considered themammalsdata frame from theMASSpackage,
giving body weights and brain weights for 62 animals. Disucssions in later chapters will consider theAnimals2data frame from therobustbase package which gives the same characterizations for a slightly dierent set of animals. In both cases, the row names for these data frames identify these animals, and the objective of this exercise is to examine the dierences
between the animals characterized in these data frames:
1a. Therownamesfunction returns a vector of row names for a data frame, and theintersectfunction computes the intersection of two sets, returning a vector of their common elements. Using these func- tions, construct and display the vectorcommonAnimalsof animal names common to both data frames. How many animals are included in this set?1b. Thesetdifffunction returns a vector of elements contained in one
set but not the other:setdiff(A, B)returns a vector of elements
in setAthat are not in setB. Use this function to display the animals present inmammalsthat are not present inAnimals2.1c. Use thesetdifffunction to display the animals present inAnimals2 that are not present inmammals.1d. Can you give a simple characterization of these dierences between these sets of animals?
Solution 1: First, it is necessary to load theMASSandrobustbasepackages to make
themammalsandAnimals2data frames available:
library(MASS) library(robustbase)
3 3 / 4
4 CHAPTER 1. DATA, EXPLORATORY ANALYSIS, AND R
Given these data frames, the solutions to the specic questions follow.1a.commonAnimals((mammals),(Animals2)) commonAnimals ## [1] "Owl monkey" "Mountain beaver" ## [3] "Cow" "Grey wolf" ## [5] "Goat" "Roe deer" ## [7] "Guinea pig" "Verbet" ## [9] "Chinchilla" "Ground squirrel" ## [11] "African giant pouched rat" "Lesser short-tailed shrew" ## [13] "Star-nosed mole" "Nine-banded armadillo" ## [15] "Tree hyrax" "N.A. opossum" ## [17] "Asian elephant" "Big brown bat" ## [19] "Donkey" "Horse" ## [21] "European hedgehog" "Cat" ## [23] "Galago" "Genet" ## [25] "Giraffe" "Gorilla" ## [27] "Grey seal" "Rock hyrax-a" ## [29] "Human" "African elephant" ## [31] "Water opossum" "Rhesus monkey" ## [33] "Kangaroo" "Yellow-bellied marmot" ## [35] "Golden hamster" "Mouse" ## [37] "Little brown bat" "Slow loris" ## [39] "Okapi" "Rabbit" ## [41] "Sheep" "Jaguar" ## [43] "Chimpanzee" "Baboon" ## [45] "Desert hedgehog" "Giant armadillo" ## [47] "Rock hyrax-b" "Raccoon" ## [49] "Rat" "E. American mole" ## [51] "Musk shrew" "Pig" ## [53] "Echidna" "Brazilian tapir" ## [55] "Tenrec" "Phalanger" ## [57] "Tree shrew" "Red fox" length(commonAnimals)
## [1] 58
1b.mammalsbut not inAnimals2are:
setdiff(rownames(mammals),(Animals2)) ## [1] "Arctic fox" "Arctic ground squirrel" ## [3] "Patas monkey" "Mole rat"
1c.Animals2but not inmammalsare:
setdiff(rownames(Animals2),(mammals)) ## [1] "Mole" "Artic ground squirrel" "Artic fox" ## [4] "Potar monkey" "Triceratops" "Dipliodocus" ## [7] "Brachiosaurus" 1d.
the two data frames:
- / 4