Setting Up Working
directory, clearing all data and memory
# Clear the workspace
rm(list = ls()) # Clear environment
gc() # Clear unused memory
## used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
## Ncells 526418 28.2 1169405 62.5 NA 669291 35.8
## Vcells 967524 7.4 8388608 64.0 32768 1840395 14.1
cat("\f") # Clear the console
dev.off() # Clear the plots
## null device
## 1
par(mfrow=c(1,1)) # dev.off()
Barplot
cars <- c(30,50, 20)
names(cars) <- c("Blue Car Sales","Red Car Sales", "Green Car Sales")
?barplot
barplot(height = cars,
main = "Car Sales by Color",
ylab = "Frequency",
col = c("blue","red", "green")
)

Pie chart
?pie
pie( x = cars,
main = "Car Sales by Color",
ylab = "Frequency",
col = c("blue","red", "green")
)

?par
par (mfrow=c(3,1))
?na.omit
Histogram
myhist <- hist(na.omit(object = airquality$Ozone),
plot=FALSE
)
Barplot (and
Polygon)
####################################################################################
?paste
names(myhist$counts) <- paste("[",myhist$breaks[1:9],", ",myhist$breaks[2:10],")", sep="")
barplot(height = myhist$counts,
col = "red",
space = 0,
main = "Histogram, Ozone Levels in NY, May-Sep 1973",
xlab = "Ozone, ppb",
ylab = "Counts"
)

barplot(height = myhist$counts/sum(myhist$counts),
col = "yellow",
space = 0,
main = "Rel. Freq. Histogram, Ozone Levels in NY, May-Sep 1973",
xlab = "Ozone, ppb",
ylab = "Proportion"
)
?polygon
mycol <- rgb(red = 0,
green = 0,
blue = 255,
max = 255,
alpha = 75,
names = "blue50"
)
polygon(x = c(0,seq(.5,8.5, by=1),8.5),
density = c(0,myhist$density*20,0),
col = mycol
)

Boxplot
summary(airquality$Ozone)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 1.00 18.00 31.50 42.13 63.25 168.00 37
library("psych")
?describe()
describe(airquality$Ozone)
## vars n mean sd median trimmed mad min max range skew kurtosis se
## X1 1 116 42.13 32.99 31.5 37.8 25.95 1 168 167 1.21 1.11 3.06
?boxplot
boxplot(na.omit(airquality$Ozone),
main="Ozone example",
horizontal=TRUE)
par(mfrow=c(1,1)) # dev.off()
airquality$Ozone
## [1] 41 36 12 18 NA 28 23 19 8 NA 7 16 11 14 18 14 34 6
## [19] 30 11 1 11 4 32 NA NA NA 23 45 115 37 NA NA NA NA NA
## [37] NA 29 NA 71 39 NA NA 23 NA NA 21 37 20 12 13 NA NA NA
## [55] NA NA NA NA NA NA NA 135 49 32 NA 64 40 77 97 97 85 NA
## [73] 10 27 NA 7 48 35 61 79 63 16 NA NA 80 108 20 52 82 50
## [91] 64 59 39 9 16 78 35 66 122 89 110 NA NA 44 28 65 NA 22
## [109] 59 23 31 44 21 9 NA 45 168 73 NA 76 118 84 85 96 78 73
## [127] 91 47 32 20 23 21 24 44 21 28 9 13 46 18 13 24 16 13
## [145] 23 36 7 14 30 NA 14 18 20
boxplot(airquality$Ozone,
main="Ozone example",
horizontal=TRUE
)

airquality$Ozone
## [1] 41 36 12 18 NA 28 23 19 8 NA 7 16 11 14 18 14 34 6
## [19] 30 11 1 11 4 32 NA NA NA 23 45 115 37 NA NA NA NA NA
## [37] NA 29 NA 71 39 NA NA 23 NA NA 21 37 20 12 13 NA NA NA
## [55] NA NA NA NA NA NA NA 135 49 32 NA 64 40 77 97 97 85 NA
## [73] 10 27 NA 7 48 35 61 79 63 16 NA NA 80 108 20 52 82 50
## [91] 64 59 39 9 16 78 35 66 122 89 110 NA NA 44 28 65 NA 22
## [109] 59 23 31 44 21 9 NA 45 168 73 NA 76 118 84 85 96 78 73
## [127] 91 47 32 20 23 21 24 44 21 28 9 13 46 18 13 24 16 13
## [145] 23 36 7 14 30 NA 14 18 20
mean(airquality$Ozone) # some functions may not be applied on vectors with NA values
## [1] NA
mean(na.omit(airquality$Ozone)) # remove the na / missing values to calculate the mean instead
## [1] 42.12931
print(na.omit(airquality$Ozone)) # na.action part tells you the index of elements that have NA. EG 5th row/element, 150th element/row
## [1] 41 36 12 18 28 23 19 8 7 16 11 14 18 14 34 6 30 11
## [19] 1 11 4 32 23 45 115 37 29 71 39 23 21 37 20 12 13 135
## [37] 49 32 64 40 77 97 97 85 10 27 7 48 35 61 79 63 16 80
## [55] 108 20 52 82 50 64 59 39 9 16 78 35 66 122 89 110 44 28
## [73] 65 22 59 23 31 44 21 9 45 168 73 76 118 84 85 96 78 73
## [91] 91 47 32 20 23 21 24 44 21 28 9 13 46 18 13 24 16 13
## [109] 23 36 7 14 30 14 18 20
## attr(,"na.action")
## [1] 5 10 25 26 27 32 33 34 35 36 37 39 42 43 45 46 52 53 54
## [20] 55 56 57 58 59 60 61 65 72 75 83 84 102 103 107 115 119 150
## attr(,"class")
## [1] "omit"
mean(iris$Sepal.Length)
## [1] 5.843333
sessionInfo()
## R version 4.2.1 (2022-06-23)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Big Sur ... 10.16
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.2/Resources/lib/libRlapack.dylib
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] psych_2.3.6
##
## loaded via a namespace (and not attached):
## [1] lattice_0.20-45 digest_0.6.33 grid_4.2.1 R6_2.5.1
## [5] nlme_3.1-161 jsonlite_1.8.7 evaluate_0.23 highr_0.10
## [9] cachem_1.0.8 rlang_1.1.2 cli_3.6.1 rstudioapi_0.14
## [13] jquerylib_0.1.4 bslib_0.5.0 rmarkdown_2.21 tools_4.2.1
## [17] parallel_4.2.1 xfun_0.39 yaml_2.3.7 fastmap_1.1.1
## [21] compiler_4.2.1 mnormt_2.1.1 htmltools_0.5.5 knitr_1.42
## [25] sass_0.4.6