##############################################
#  Working with data
##############################################

## --------------------------------------------------------------------------------------------------------------------------------------------------
path <- dirname(rstudioapi::getActiveDocumentContext()$path)
setwd(path)


## --------------------------------------------------------------------------------------------------------------------------------------------------
path_secura <- file.path('../data/SecuraRe.txt')
secura_re <- read.table(path_secura, header = TRUE)
str(secura_re)

## --------------------------------------------------------------------------------------------------------------------------------------------------
path_urbanpop <- file.path("../data/urbanpop.xlsx")
readxl::excel_sheets(path_urbanpop) # list sheet names with `excel_sheets()`

## --------------------------------------------------------------------------------------------------------------------------------------------------
pop_1 <- readxl::read_excel(path_urbanpop, sheet = 1) 
pop_2 <- readxl::read_excel(path_urbanpop, sheet = "1967-1974") 

## --------------------------------------------------------------------------------------------------------------------------------------------------
names(pop_1)
names(pop_2)

## Your Turn!
## --------------------------------------------------------------------------------------------------------------------------------------------------




## --------------------------------------------------------------------------------------------------------------------------------------------------
path_pc <- file.path('../data/PC_data.txt')
mtpl_tidy <- readr::read_table2(path_pc, col_types = 'iidddfffffiiiiffdd')
str(mtpl_tidy, give.attr = FALSE)

## --------------------------------------------------------------------------------------------------------------------------------------------------
# get a summary
summary(mtpl_tidy$AGEPH)

# check if variable is numeric
is.numeric(mtpl_tidy$AGEPH) 

# get mean
mean(mtpl_tidy$AGEPH) 

# get variance
var(mtpl_tidy$AGEPH)             

## --------------------------------------------------------------------------------------------------------------------------------------------------
hist(mtpl_tidy$AGEPH, freq = FALSE,
     nclass = 20, col = "light blue", main = "My first histogram")
lines(density(mtpl_tidy$AGEPH), col = "red")

## --------------------------------------------------------------------------------------------------------------------------------------------------
# check if variable is a factor
is.factor(mtpl_tidy$COVERAGE) 

# get observation counts
table(mtpl_tidy$COVERAGE) 

# get proportional observation counts
prop.table(table(mtpl_tidy$COVERAGE))

## --------------------------------------------------------------------------------------------------------------------------------------------------
barplot(table(mtpl_tidy$COVERAGE),
        col = "light blue")

## --------------------------------------------------------------------------------------------------------------------------------------------------
table(mtpl_tidy$SEX, mtpl_tidy$FUEL)

prop.table(table(mtpl_tidy$SEX, mtpl_tidy$FUEL))

prop.table(table(mtpl_tidy$SEX, mtpl_tidy$FUEL), 1)

## --------------------------------------------------------------------------------------------------------------------------------------------------
plot(FUEL ~ SEX, data = mtpl_tidy)

## --------------------------------------------------------------------------------------------------------------------------------------------------
tapply(mtpl_tidy$POWER, mtpl_tidy$USE, mean)

tapply(mtpl_tidy$POWER,
       list(mtpl_tidy$USE, mtpl_tidy$FUEL), mean)

## --------------------------------------------------------------------------------------------------------------------------------------------------
boxplot(POWER ~ USE, data = mtpl_tidy)

## --------------------------------------------------------------------------------------------------------------------------------------------------
subset(diamonds, cut == "Ideal") # rows
diamonds[, c("carat", "cut", "color", "clarity")] # columns

## --------------------------------------------------------------------------------------------------------------------------------------------------
diamonds$price_per_carat <- diamonds$price/diamonds$carat

## --------------------------------------------------------------------------------------------------------------------------------------------------
aggregate(price ~ cut, diamonds, mean)

## --------------------------------------------------------------------------------------------------------------------------------------------------
aggregate(price ~ cut + color, diamonds, mean)

## --------------------------------------------------------------------------------------------------------------------------------------------------
str(mtcars, list.len = 5)

str(as_tibble(mtcars), list.len = 5)

## --------------------------------------------------------------------------------------------------------------------------------------------------
diamonds %>% subset(cut == "Ideal")

## --------------------------------------------------------------------------------------------------------------------------------------------------
subset(diamonds, cut == "Ideal")

## --------------------------------------------------------------------------------------------------------------------------------------------------
mtcars %>% filter(hp > 200)

## --------------------------------------------------------------------------------------------------------------------------------------------------
mtcars %>% filter(hp > 200) %>% mutate(hp_per_wt = hp / wt)


## --------------------------------------------------------------------------------------------------------------------------------------------------
mtcars %>% summarise(hp_mean = mean(hp),
                     hp_sd = sd(hp),
                     wt_mean = mean(wt),
                     wt_sd = sd(wt))

## --------------------------------------------------------------------------------------------------------------------------------------------------
mtcars %>% summarise(across(c(hp, wt),
                            list(mean2 = mean, sd2 = sd)))

## --------------------------------------------------------------------------------------------------------------------------------------------------
## mtcars %>% group_by(cyl) %>% summarise(hp_mean = mean(hp),
##                                        disp_mean = mean(disp))

## --------------------------------------------------------------------------------------------------------------------------------------------------
mtcars %>% group_by(cyl) %>% summarise(hp_mean = mean(hp),
                                       disp_mean = mean(disp)) %>% as.data.frame()

## Your Turn!
## --------------------------------------------------------------------------------------------------------------------------------------------------







## --------------------------------------------------------------------------------------------------------------------------------------------------
