#' # R101: base R + Rstudio
#' 
#' * R-files
#' * Rstudio
#'     + Panes
#' * Getting help
#'     + `?help`
#'     + `help.search("linear regression")`
#'     + Search engine / social media
#' * Libraries
#'     + `library()`
#'     + CRAN
#'     + GitHub
#'     + Your own (Advanced R: <https://adv-r.hadley.nz/>)
#'
#' # Reading data
#' 
#' * `crickets` (Walker, 1962: <https://doi.org/10.1093/aesa/55.3.303>)
#' 
crickets <- read.table("data/crickets.txt", header = TRUE)
head(crickets)
str(crickets)


mean(crickets$temp)
sum(crickets$temp) / length(crickets$temp)
sum(crickets$temp) / nrow(crickets)
sd(crickets$temp)

#' 
#' * <https://CRAN.R-project.org/package=rio> / <https://github.com/leeper/rio>
#' * Broman and Woo (2017), "Data Organization in Spreadsheets": <https://doi.org/10.1080/00031305.2017.1375989>
#'     + <http://kbroman.org/dataorg/>
#'
#' Data formats:
#' 
#' * File -> Import Dataset
#' * <http://r4ds.had.co.nz/data-import.html>
#' * <http://r4ds.had.co.nz/data-import.html#other-types-of-data>
#' * `haven`: SPSS, Stata, SAS
#' * `readxl`: Excel (both `.xls` and `.xlsx`)
#' * `DBI` together with `RMySQL`, `RSQLite`, `RPostgreSQL` and others: Databases
#' * Hierarchical data: `jsonlite`: json; `xml2`: XML
#' * <https://github.com/leeper/rio>
#' * <https://cran.r-project.org/doc/manuals/r-release/R-data.html>
#'
#'
#' # Visualisations
#' 
#' * `cars`
#' * `ToothGrowth`
#' * `crickets`
#' 
#' Functions: 
#'
#' `plot()`

plot(crickets$temp, crickets$pps)
plot(pps ~ temp, crickets)
plot(pps ~ temp, crickets, xlab = "Temperature", ylab = "Pulses per second")

#' `hist()` (`nclass.Sturges()`, `nclass.scott()`, `nclass.FD()`)

hist(crickets$temp)
hist(crickets$temp, breaks = "FD")
nclass.FD(crickets$temp)
hist(crickets$temp, breaks = 4)

set.seed(1)
xs <- runif(500)

for (brks in c(5, 10, 25, 50)) {
  hist(xs, breaks = brks, main = brks)
}

nclass.FD(xs)

#' * `boxplot()`

boxplot(crickets$temp)
boxplot(temp ~ species, crickets)

table(crickets$species)

#' 
#'
#' # Data types
#' 
#' `str()`
str(crickets)
#' 
#' data > df > list > vector

crickets[[1]]
crickets[, 1]
crickets$species
