-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSelectingCases.R
72 lines (47 loc) · 1.75 KB
/
SelectingCases.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File: SelectingCases.R
# Course: R: An Introduction (with RStudio)
# INSTALL AND LOAD PACKAGES ################################
library(datasets) # Load/unload base packages manually
# LOAD DATA ################################################
head(iris)
# ALL DATA #################################################
hist(iris$Petal.Length)
summary(iris$Petal.Length)
summary(iris$Species) # Get names and n for each species
# SELECT BY CATEGORY #######################################
# Versicolor
hist(iris$Petal.Length[iris$Species == "versicolor"],
main = "Petal Length: Versicolor")
# Virginica
hist(iris$Petal.Length[iris$Species == "virginica"],
main = "Petal Length: Virginica")
# Setosa
hist(iris$Petal.Length[iris$Species == "setosa"],
main = "Petal Length: Setosa")
# SELECT BY VALUE ##########################################
# Short petals only (all Setosa)
hist(iris$Petal.Length[iris$Petal.Length < 2],
main = "Petal Length < 2")
# MULTIPLE SELECTORS #######################################
# Short Virginica petals only
hist(iris$Petal.Length[iris$Species == "virginica" &
iris$Petal.Length < 5.5],
main = "Petal Length: Short Virginica")
# CREATE SUBSAMPLE #########################################
# Format: data[rows, columns]
# Leave rows or columns blank to select all
i.setosa <- iris[iris$Species == "setosa", ]
# EXPLORE SUBSAMPLE ########################################
head(i.setosa)
summary(i.setosa$Petal.Length)
hist(i.setosa$Petal.Length)
# CLEAN UP #################################################
# Clear environment
rm(list = ls())
# Clear packages
detach("package:datasets", unload = TRUE) # For base
# Clear plots
dev.off() # But only if there IS a plot
# Clear console
cat("\014") # ctrl+L
# Clear mind :)