################################################################################
##                                                                            ##
##                           BStats - LAB 1                                   ##
##                                                                            ##
################################################################################

##-- Package installation ------------------------------------------------------

# Install tidyverse 
install.packages("tidyverse")
# Note: you only need to install each package once


##-- Load packages -------------------------------------------------------------
library(tidyverse)


##-- Finding help --------------------------------------------------------------

# Details about your R Session
sessionInfo()

# Getting help on a specific function
?sessionInfo

##-- Using the Console as a calculator -----------------------------------------

1+1
2*(10-2)*4
8^(1/3)
sqrt(25) # square root
exp(2)   # e^2
log(1)   # natural log

##-- Creating objects ----------------------------------------------------------

# Create an object containing text
name <- "John"
name   # output "John"

# Print vs. assign

# Create an object containing a number
age <- 40
age    # output 40

# Interact with object
age + 10 

# Overwrite object
age <- age + 10
age   # output new object

# Create new object
my_class <- 'This class is awesome'

# R is case sensitive; try running:
My_class

##-- Functions -----------------------------------------------------------------

vec <- 1:10
mean(vec)
seq(1, 10, 2)

# We can apply functions to every element of a vector
c(3, 5) + 1

##-- Exercise 2 ----------------------------------------------------------------
# Build a vector starting in -10, ending in 89, 3 by 3:


##-- Exercise 3 ----------------------------------------------------------------
# apply a mean to this vector ignoring the missing value:
vec2 <- c(3, NA, 16, 20)


##-- Exercise 4 ----------------------------------------------------------------
# Country A has a GDP of 100 in t = 0 and grows 3% a year. 
# Build the time series of that GDP
# hint: remember the compound rate formula


##-- The Piping ----------------------------------------------------------------
# The pipe: `%>%`. 
# Shortcuts: `Ctrl` + `Shift` + `M` (Windows) or `Cmd` + `Shift` + `M` (Mac).
round(mean(c(1, 5, 7, NA), na.rm = TRUE), 2)
# VS.
c(1, 5, 7, NA) %>% mean(na.rm = TRUE) %>% round(2)


##-- Working Directory ---------------------------------------------------------

getwd()
# This will be different inside and outside of an RProject

##-- Read data -----------------------------------------------------------------

# csv files
df <- read_csv("data/qp_sample.csv")

# alternatively:
df <- read_csv("C:/path/to/directory/containing/the/files/qp_sample.csv")


