This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
—– this is the place where package is loaded.
require(ggplot2)
## Loading required package: ggplot2
——we are printing the data of msleep because this has two categorical values and two numeric values.genus and vore are two categorical value and sleep_total and awake are two numerical values
msleep
## name genus vore order
## 1 Cheetah Acinonyx carni Carnivora
## 2 Owl monkey Aotus omni Primates
## 3 Mountain beaver Aplodontia herbi Rodentia
## 4 Greater short-tailed shrew Blarina omni Soricomorpha
## 5 Cow Bos herbi Artiodactyla
## 6 Three-toed sloth Bradypus herbi Pilosa
## 7 Northern fur seal Callorhinus carni Carnivora
## 8 Vesper mouse Calomys <NA> Rodentia
## 9 Dog Canis carni Carnivora
## 10 Roe deer Capreolus herbi Artiodactyla
## 11 Goat Capri herbi Artiodactyla
## 12 Guinea pig Cavis herbi Rodentia
## 13 Grivet Cercopithecus omni Primates
## 14 Chinchilla Chinchilla herbi Rodentia
## 15 Star-nosed mole Condylura omni Soricomorpha
## 16 African giant pouched rat Cricetomys omni Rodentia
## 17 Lesser short-tailed shrew Cryptotis omni Soricomorpha
## 18 Long-nosed armadillo Dasypus carni Cingulata
## 19 Tree hyrax Dendrohyrax herbi Hyracoidea
## 20 North American Opossum Didelphis omni Didelphimorphia
## 21 Asian elephant Elephas herbi Proboscidea
## 22 Big brown bat Eptesicus insecti Chiroptera
## 23 Horse Equus herbi Perissodactyla
## 24 Donkey Equus herbi Perissodactyla
## 25 European hedgehog Erinaceus omni Erinaceomorpha
## 26 Patas monkey Erythrocebus omni Primates
## 27 Western american chipmunk Eutamias herbi Rodentia
## 28 Domestic cat Felis carni Carnivora
## 29 Galago Galago omni Primates
## 30 Giraffe Giraffa herbi Artiodactyla
## 31 Pilot whale Globicephalus carni Cetacea
## 32 Gray seal Haliochoerus carni Carnivora
## 33 Gray hyrax Heterohyrax herbi Hyracoidea
## 34 Human Homo omni Primates
## 35 Mongoose lemur Lemur herbi Primates
## 36 African elephant Loxodonta herbi Proboscidea
## 37 Thick-tailed opposum Lutreolina carni Didelphimorphia
## 38 Macaque Macaca omni Primates
## 39 Mongolian gerbil Meriones herbi Rodentia
## 40 Golden hamster Mesocricetus herbi Rodentia
## 41 Vole Microtus herbi Rodentia
## 42 House mouse Mus herbi Rodentia
## 43 Little brown bat Myotis insecti Chiroptera
## 44 Round-tailed muskrat Neofiber herbi Rodentia
## 45 Slow loris Nyctibeus carni Primates
## 46 Degu Octodon herbi Rodentia
## 47 Northern grasshopper mouse Onychomys carni Rodentia
## 48 Rabbit Oryctolagus herbi Lagomorpha
## 49 Sheep Ovis herbi Artiodactyla
## 50 Chimpanzee Pan omni Primates
## 51 Tiger Panthera carni Carnivora
## 52 Jaguar Panthera carni Carnivora
## 53 Lion Panthera carni Carnivora
## 54 Baboon Papio omni Primates
## 55 Desert hedgehog Paraechinus <NA> Erinaceomorpha
## 56 Potto Perodicticus omni Primates
## 57 Deer mouse Peromyscus <NA> Rodentia
## 58 Phalanger Phalanger <NA> Diprotodontia
## 59 Caspian seal Phoca carni Carnivora
## 60 Common porpoise Phocoena carni Cetacea
## 61 Potoroo Potorous herbi Diprotodontia
## 62 Giant armadillo Priodontes insecti Cingulata
## 63 Rock hyrax Procavia <NA> Hyracoidea
## 64 Laboratory rat Rattus herbi Rodentia
## 65 African striped mouse Rhabdomys omni Rodentia
## 66 Squirrel monkey Saimiri omni Primates
## 67 Eastern american mole Scalopus insecti Soricomorpha
## 68 Cotton rat Sigmodon herbi Rodentia
## 69 Mole rat Spalax <NA> Rodentia
## 70 Arctic ground squirrel Spermophilus herbi Rodentia
## 71 Thirteen-lined ground squirrel Spermophilus herbi Rodentia
## 72 Golden-mantled ground squirrel Spermophilus herbi Rodentia
## 73 Musk shrew Suncus <NA> Soricomorpha
## 74 Pig Sus omni Artiodactyla
## 75 Short-nosed echidna Tachyglossus insecti Monotremata
## 76 Eastern american chipmunk Tamias herbi Rodentia
## 77 Brazilian tapir Tapirus herbi Perissodactyla
## 78 Tenrec Tenrec omni Afrosoricida
## 79 Tree shrew Tupaia omni Scandentia
## 80 Bottle-nosed dolphin Tursiops carni Cetacea
## 81 Genet Genetta carni Carnivora
## 82 Arctic fox Vulpes carni Carnivora
## 83 Red fox Vulpes carni Carnivora
## conservation sleep_total sleep_rem sleep_cycle awake brainwt bodywt
## 1 lc 12.1 NA NA 11.90 NA 50.000
## 2 <NA> 17.0 1.8 NA 7.00 0.01550 0.480
## 3 nt 14.4 2.4 NA 9.60 NA 1.350
## 4 lc 14.9 2.3 0.1333333 9.10 0.00029 0.019
## 5 domesticated 4.0 0.7 0.6666667 20.00 0.42300 600.000
## 6 <NA> 14.4 2.2 0.7666667 9.60 NA 3.850
## 7 vu 8.7 1.4 0.3833333 15.30 NA 20.490
## 8 <NA> 7.0 NA NA 17.00 NA 0.045
## 9 domesticated 10.1 2.9 0.3333333 13.90 0.07000 14.000
## 10 lc 3.0 NA NA 21.00 0.09820 14.800
## 11 lc 5.3 0.6 NA 18.70 0.11500 33.500
## 12 domesticated 9.4 0.8 0.2166667 14.60 0.00550 0.728
## 13 lc 10.0 0.7 NA 14.00 NA 4.750
## 14 domesticated 12.5 1.5 0.1166667 11.50 0.00640 0.420
## 15 lc 10.3 2.2 NA 13.70 0.00100 0.060
## 16 <NA> 8.3 2.0 NA 15.70 0.00660 1.000
## 17 lc 9.1 1.4 0.1500000 14.90 0.00014 0.005
## 18 lc 17.4 3.1 0.3833333 6.60 0.01080 3.500
## 19 lc 5.3 0.5 NA 18.70 0.01230 2.950
## 20 lc 18.0 4.9 0.3333333 6.00 0.00630 1.700
## 21 en 3.9 NA NA 20.10 4.60300 2547.000
## 22 lc 19.7 3.9 0.1166667 4.30 0.00030 0.023
## 23 domesticated 2.9 0.6 1.0000000 21.10 0.65500 521.000
## 24 domesticated 3.1 0.4 NA 20.90 0.41900 187.000
## 25 lc 10.1 3.5 0.2833333 13.90 0.00350 0.770
## 26 lc 10.9 1.1 NA 13.10 0.11500 10.000
## 27 <NA> 14.9 NA NA 9.10 NA 0.071
## 28 domesticated 12.5 3.2 0.4166667 11.50 0.02560 3.300
## 29 <NA> 9.8 1.1 0.5500000 14.20 0.00500 0.200
## 30 cd 1.9 0.4 NA 22.10 NA 899.995
## 31 cd 2.7 0.1 NA 21.35 NA 800.000
## 32 lc 6.2 1.5 NA 17.80 0.32500 85.000
## 33 lc 6.3 0.6 NA 17.70 0.01227 2.625
## 34 <NA> 8.0 1.9 1.5000000 16.00 1.32000 62.000
## 35 vu 9.5 0.9 NA 14.50 NA 1.670
## 36 vu 3.3 NA NA 20.70 5.71200 6654.000
## 37 lc 19.4 6.6 NA 4.60 NA 0.370
## 38 <NA> 10.1 1.2 0.7500000 13.90 0.17900 6.800
## 39 lc 14.2 1.9 NA 9.80 NA 0.053
## 40 en 14.3 3.1 0.2000000 9.70 0.00100 0.120
## 41 <NA> 12.8 NA NA 11.20 NA 0.035
## 42 nt 12.5 1.4 0.1833333 11.50 0.00040 0.022
## 43 <NA> 19.9 2.0 0.2000000 4.10 0.00025 0.010
## 44 nt 14.6 NA NA 9.40 NA 0.266
## 45 <NA> 11.0 NA NA 13.00 0.01250 1.400
## 46 lc 7.7 0.9 NA 16.30 NA 0.210
## 47 lc 14.5 NA NA 9.50 NA 0.028
## 48 domesticated 8.4 0.9 0.4166667 15.60 0.01210 2.500
## 49 domesticated 3.8 0.6 NA 20.20 0.17500 55.500
## 50 <NA> 9.7 1.4 1.4166667 14.30 0.44000 52.200
## 51 en 15.8 NA NA 8.20 NA 162.564
## 52 nt 10.4 NA NA 13.60 0.15700 100.000
## 53 vu 13.5 NA NA 10.50 NA 161.499
## 54 <NA> 9.4 1.0 0.6666667 14.60 0.18000 25.235
## 55 lc 10.3 2.7 NA 13.70 0.00240 0.550
## 56 lc 11.0 NA NA 13.00 NA 1.100
## 57 <NA> 11.5 NA NA 12.50 NA 0.021
## 58 <NA> 13.7 1.8 NA 10.30 0.01140 1.620
## 59 vu 3.5 0.4 NA 20.50 NA 86.000
## 60 vu 5.6 NA NA 18.45 NA 53.180
## 61 <NA> 11.1 1.5 NA 12.90 NA 1.100
## 62 en 18.1 6.1 NA 5.90 0.08100 60.000
## 63 lc 5.4 0.5 NA 18.60 0.02100 3.600
## 64 lc 13.0 2.4 0.1833333 11.00 0.00190 0.320
## 65 <NA> 8.7 NA NA 15.30 NA 0.044
## 66 <NA> 9.6 1.4 NA 14.40 0.02000 0.743
## 67 lc 8.4 2.1 0.1666667 15.60 0.00120 0.075
## 68 <NA> 11.3 1.1 0.1500000 12.70 0.00118 0.148
## 69 <NA> 10.6 2.4 NA 13.40 0.00300 0.122
## 70 lc 16.6 NA NA 7.40 0.00570 0.920
## 71 lc 13.8 3.4 0.2166667 10.20 0.00400 0.101
## 72 lc 15.9 3.0 NA 8.10 NA 0.205
## 73 <NA> 12.8 2.0 0.1833333 11.20 0.00033 0.048
## 74 domesticated 9.1 2.4 0.5000000 14.90 0.18000 86.250
## 75 <NA> 8.6 NA NA 15.40 0.02500 4.500
## 76 <NA> 15.8 NA NA 8.20 NA 0.112
## 77 vu 4.4 1.0 0.9000000 19.60 0.16900 207.501
## 78 <NA> 15.6 2.3 NA 8.40 0.00260 0.900
## 79 <NA> 8.9 2.6 0.2333333 15.10 0.00250 0.104
## 80 <NA> 5.2 NA NA 18.80 NA 173.330
## 81 <NA> 6.3 1.3 NA 17.70 0.01750 2.000
## 82 <NA> 12.5 NA NA 11.50 0.04450 3.380
## 83 <NA> 9.8 2.4 0.3500000 14.20 0.05040 4.230
-identified which one is categorical and which are numerical
str(msleep)
## 'data.frame': 83 obs. of 11 variables:
## $ name : chr "Cheetah" "Owl monkey" "Mountain beaver" "Greater short-tailed shrew" ...
## $ genus : chr "Acinonyx" "Aotus" "Aplodontia" "Blarina" ...
## $ vore : Factor w/ 4 levels "carni","herbi",..: 1 4 2 4 2 2 1 NA 1 2 ...
## $ order : chr "Carnivora" "Primates" "Rodentia" "Soricomorpha" ...
## $ conservation: Factor w/ 7 levels "","cd","domesticated",..: 5 NA 6 5 3 NA 7 NA 3 5 ...
## $ sleep_total : num 12.1 17 14.4 14.9 4 14.4 8.7 7 10.1 3 ...
## $ sleep_rem : num NA 1.8 2.4 2.3 0.7 2.2 1.4 NA 2.9 NA ...
## $ sleep_cycle : num NA NA NA 0.133 0.667 ...
## $ awake : num 11.9 7 9.6 9.1 20 9.6 15.3 17 13.9 21 ...
## $ brainwt : num NA 0.0155 NA 0.00029 0.423 NA NA NA 0.07 0.0982 ...
## $ bodywt : num 50 0.48 1.35 0.019 600 ...
– Generated summary level descriptive statistics: Show the mean, median, 25th and 75th quartiles, min, and max for each of the applicable variables in msleep.
summary(msleep)
## name genus vore order
## Length:83 Length:83 carni :19 Length:83
## Class :character Class :character herbi :32 Class :character
## Mode :character Mode :character insecti: 5 Mode :character
## omni :20
## NA's : 7
##
##
## conservation sleep_total sleep_rem sleep_cycle
## lc :27 Min. : 1.90 Min. :0.100 Min. :0.1167
## domesticated:10 1st Qu.: 7.85 1st Qu.:0.900 1st Qu.:0.1833
## vu : 7 Median :10.10 Median :1.500 Median :0.3333
## en : 4 Mean :10.43 Mean :1.875 Mean :0.4396
## nt : 4 3rd Qu.:13.75 3rd Qu.:2.400 3rd Qu.:0.5792
## (Other) : 2 Max. :19.90 Max. :6.600 Max. :1.5000
## NA's :29 NA's :22 NA's :51
## awake brainwt bodywt
## Min. : 4.10 Min. :0.00014 Min. : 0.005
## 1st Qu.:10.25 1st Qu.:0.00290 1st Qu.: 0.174
## Median :13.90 Median :0.01240 Median : 1.670
## Mean :13.57 Mean :0.28158 Mean : 166.136
## 3rd Qu.:16.15 3rd Qu.:0.12550 3rd Qu.: 41.750
## Max. :22.10 Max. :5.71200 Max. :6654.000
## NA's :27
–Determine the frequency for each of vore - 1st categorical value
table(msleep$vore)
##
## carni herbi insecti omni
## 19 32 5 20
–Determine the frequency for each of genus - 2nd categorical value
table(msleep$genus)
##
## Acinonyx Aotus Aplodontia Blarina Bos
## 1 1 1 1 1
## Bradypus Callorhinus Calomys Canis Capreolus
## 1 1 1 1 1
## Capri Cavis Cercopithecus Chinchilla Condylura
## 1 1 1 1 1
## Cricetomys Cryptotis Dasypus Dendrohyrax Didelphis
## 1 1 1 1 1
## Elephas Eptesicus Equus Erinaceus Erythrocebus
## 1 1 2 1 1
## Eutamias Felis Galago Genetta Giraffa
## 1 1 1 1 1
## Globicephalus Haliochoerus Heterohyrax Homo Lemur
## 1 1 1 1 1
## Loxodonta Lutreolina Macaca Meriones Mesocricetus
## 1 1 1 1 1
## Microtus Mus Myotis Neofiber Nyctibeus
## 1 1 1 1 1
## Octodon Onychomys Oryctolagus Ovis Pan
## 1 1 1 1 1
## Panthera Papio Paraechinus Perodicticus Peromyscus
## 3 1 1 1 1
## Phalanger Phoca Phocoena Potorous Priodontes
## 1 1 1 1 1
## Procavia Rattus Rhabdomys Saimiri Scalopus
## 1 1 1 1 1
## Sigmodon Spalax Spermophilus Suncus Sus
## 1 1 3 1 1
## Tachyglossus Tamias Tapirus Tenrec Tupaia
## 1 1 1 1 1
## Tursiops Vulpes
## 1 2
```
–Determine the frequency for each of the vore, by genus.
# Run the table() function against two categorical variables.
table(msleep$genus, msleep$vore)
##
## carni herbi insecti omni
## Acinonyx 1 0 0 0
## Aotus 0 0 0 1
## Aplodontia 0 1 0 0
## Blarina 0 0 0 1
## Bos 0 1 0 0
## Bradypus 0 1 0 0
## Callorhinus 1 0 0 0
## Calomys 0 0 0 0
## Canis 1 0 0 0
## Capreolus 0 1 0 0
## Capri 0 1 0 0
## Cavis 0 1 0 0
## Cercopithecus 0 0 0 1
## Chinchilla 0 1 0 0
## Condylura 0 0 0 1
## Cricetomys 0 0 0 1
## Cryptotis 0 0 0 1
## Dasypus 1 0 0 0
## Dendrohyrax 0 1 0 0
## Didelphis 0 0 0 1
## Elephas 0 1 0 0
## Eptesicus 0 0 1 0
## Equus 0 2 0 0
## Erinaceus 0 0 0 1
## Erythrocebus 0 0 0 1
## Eutamias 0 1 0 0
## Felis 1 0 0 0
## Galago 0 0 0 1
## Genetta 1 0 0 0
## Giraffa 0 1 0 0
## Globicephalus 1 0 0 0
## Haliochoerus 1 0 0 0
## Heterohyrax 0 1 0 0
## Homo 0 0 0 1
## Lemur 0 1 0 0
## Loxodonta 0 1 0 0
## Lutreolina 1 0 0 0
## Macaca 0 0 0 1
## Meriones 0 1 0 0
## Mesocricetus 0 1 0 0
## Microtus 0 1 0 0
## Mus 0 1 0 0
## Myotis 0 0 1 0
## Neofiber 0 1 0 0
## Nyctibeus 1 0 0 0
## Octodon 0 1 0 0
## Onychomys 1 0 0 0
## Oryctolagus 0 1 0 0
## Ovis 0 1 0 0
## Pan 0 0 0 1
## Panthera 3 0 0 0
## Papio 0 0 0 1
## Paraechinus 0 0 0 0
## Perodicticus 0 0 0 1
## Peromyscus 0 0 0 0
## Phalanger 0 0 0 0
## Phoca 1 0 0 0
## Phocoena 1 0 0 0
## Potorous 0 1 0 0
## Priodontes 0 0 1 0
## Procavia 0 0 0 0
## Rattus 0 1 0 0
## Rhabdomys 0 0 0 1
## Saimiri 0 0 0 1
## Scalopus 0 0 1 0
## Sigmodon 0 1 0 0
## Spalax 0 0 0 0
## Spermophilus 0 3 0 0
## Suncus 0 0 0 0
## Sus 0 0 0 1
## Tachyglossus 0 0 1 0
## Tamias 0 1 0 0
## Tapirus 0 1 0 0
## Tenrec 0 0 0 1
## Tupaia 0 0 0 1
## Tursiops 1 0 0 0
## Vulpes 2 0 0 0
–Determine the frequency for each of the genus by vore.
table(msleep$vore, msleep$genus)
##
## Acinonyx Aotus Aplodontia Blarina Bos Bradypus Callorhinus
## carni 1 0 0 0 0 0 1
## herbi 0 0 1 0 1 1 0
## insecti 0 0 0 0 0 0 0
## omni 0 1 0 1 0 0 0
##
## Calomys Canis Capreolus Capri Cavis Cercopithecus Chinchilla
## carni 0 1 0 0 0 0 0
## herbi 0 0 1 1 1 0 1
## insecti 0 0 0 0 0 0 0
## omni 0 0 0 0 0 1 0
##
## Condylura Cricetomys Cryptotis Dasypus Dendrohyrax Didelphis
## carni 0 0 0 1 0 0
## herbi 0 0 0 0 1 0
## insecti 0 0 0 0 0 0
## omni 1 1 1 0 0 1
##
## Elephas Eptesicus Equus Erinaceus Erythrocebus Eutamias Felis
## carni 0 0 0 0 0 0 1
## herbi 1 0 2 0 0 1 0
## insecti 0 1 0 0 0 0 0
## omni 0 0 0 1 1 0 0
##
## Galago Genetta Giraffa Globicephalus Haliochoerus Heterohyrax
## carni 0 1 0 1 1 0
## herbi 0 0 1 0 0 1
## insecti 0 0 0 0 0 0
## omni 1 0 0 0 0 0
##
## Homo Lemur Loxodonta Lutreolina Macaca Meriones Mesocricetus
## carni 0 0 0 1 0 0 0
## herbi 0 1 1 0 0 1 1
## insecti 0 0 0 0 0 0 0
## omni 1 0 0 0 1 0 0
##
## Microtus Mus Myotis Neofiber Nyctibeus Octodon Onychomys
## carni 0 0 0 0 1 0 1
## herbi 1 1 0 1 0 1 0
## insecti 0 0 1 0 0 0 0
## omni 0 0 0 0 0 0 0
##
## Oryctolagus Ovis Pan Panthera Papio Paraechinus Perodicticus
## carni 0 0 0 3 0 0 0
## herbi 1 1 0 0 0 0 0
## insecti 0 0 0 0 0 0 0
## omni 0 0 1 0 1 0 1
##
## Peromyscus Phalanger Phoca Phocoena Potorous Priodontes Procavia
## carni 0 0 1 1 0 0 0
## herbi 0 0 0 0 1 0 0
## insecti 0 0 0 0 0 1 0
## omni 0 0 0 0 0 0 0
##
## Rattus Rhabdomys Saimiri Scalopus Sigmodon Spalax Spermophilus
## carni 0 0 0 0 0 0 0
## herbi 1 0 0 0 1 0 3
## insecti 0 0 0 1 0 0 0
## omni 0 1 1 0 0 0 0
##
## Suncus Sus Tachyglossus Tamias Tapirus Tenrec Tupaia Tursiops
## carni 0 0 0 0 0 0 0 1
## herbi 0 0 0 1 1 0 0 0
## insecti 0 0 1 0 0 0 0 0
## omni 0 1 0 0 0 1 1 0
##
## Vulpes
## carni 2
## herbi 0
## insecti 0
## omni 0
—- Create a graph for a single numeric variable.
boxplot(msleep$sleep_total)
hist(msleep$sleep_total)
— Create a scatterplot of two numeric variables.
plot(msleep$sleep_total ~ msleep$awake)