##
#
# MEBn5033 Assignment 01 - Data manipulation and descriptive statistics
#
##

### Instructions ###

# Save this script under the new name with your surname appended 
# E.g.: MVZn5033_01_Assignment_Ocelik.R

# Write the commands directly to this newly saved script
# It sufficient to use just one solution to each task
# Each task is awarded by 1 point when done correctly
# Upload the script into the corresponding Homework Vault when you are finished 

# ?function_names gives you a hint what function to use
# It also opens, often a quite comprehensive, documentation of the function
# Do not copy paste the complete function description, typically we are OK with 1-4 arguments 


#
# 1. Read in dataset from the "survey.csv" file  
#

?read.csv
data <- read.csv("survey.csv", header = T)

# RECOMMENDED: use "attach" function for easier manipulation of the variables
# Also, explore the dataset by using "str", "head", and "tail" functions
?attach
attach(data)

?str
str(data)
?head
head(data)
?tail
tail(data)

#
# 2. Describe the "Age" variable by using mean, median, range, and standard deviation 
#

?mean
mean(Age)
?median
median(Age)
?range
range(Age)
max(Age) - min(Age)
?sd
sd(Age)

#
# 3. Calculate 10th, 25th, 50th, 75th, and 90th percentile of the "Age"
# HINT: use "probs" argument of the "quantile" function to specify arguments
#

?quantile
quantile(Age)
quantile(Age, probs = c(0, 0.10, 0.25, 0.5, 0.75, 0.9, 1))

#
# 4. Calculate squared devitions from mean for the "Age" variable 
# and save them into a new object called "squared.dev"
# HINT: use ^ operator for exponentiation
#

squared.dev <- (Age - mean(Age))^2
squared.dev

round(squared.dev, 3) # using "round" function to limit the result to 3 decimal places

#
# 5. Sum up the squared deviations, divide them by (number of cases (rows) - 1)
# and save the result into a separate object called "variance"
# and compare it with the result of "var" function, you should see identical numbers
# HINT: use "nrow" function to determine number of cases in the dataset
#

?sum
?nrow
variance <- sum(squared.dev) / (nrow(data) - 1)
variance
?var
var(Age)

#
# 6. Take a square root of the "variance" and compare it with standard deviation function ("sd")
#

?sqrt
sqrt(variance)
?sd
sd(Age)

#
# 7. Calculate coefficent of variability and z-scores (x - mean(X)) / sd(x) for the "Age" variable 
# compare the z-scores calculation with function "scale", you should see identifical results
#

# coefficient of variability
sd(Age) / mean(Age)

# z-scores 
(Age - mean(Age)) / sd(Age)

?scale
scale(Age)

#
# 8. Use "complete.cases" to remove NA values and create a subset of the original dataset that does not contain NA values 
# save it into a new data.frame called "data.NA"
# HINT: combine square brackets indexing with "complete.cases" function
#

?complete.cases
data.NA <- data[complete.cases(data),]
str(data.NA)

#
# 9. Calculate mean, median, std. deviation, lower, middle, and upper quartile values, and z-scores 
# for "Height" variable from the "data.NA" dataset
#

mean(data.NA$Height)

# another option is to use "na.rm" (NA remove) argument set to TRUTH (T) to exclude NA values
mean(Height, na.rm = T)
# This gives us slightly different results since there are NAs 
# which do not belong to the Height variable - and we thus lose some information (valid values for the Height)

median(data.NA$Height)
median(Height, na.rm = T) # again, note the difference

sd(data.NA$Height)
sd(Height, na.rm = T) # again, note the difference

quantile(data.NA$Height, probs=c(0.25, 0.5, 0.75))
quantile(Height, probs=c(0.25, 0.5, 0.75), na.rm = T) # again, note the difference

scale(data.NA$Height) 
scale(Height) # the na.rm function is not available here, instead NAs are kept in the output

#
# 10. Save the "data.NA" dataset into a *.txt file called "data.NA.txt"
# Exclude rownames and include variable names (colnames) in the "data.NA.txt"
#

?write.table
write.table(data.NA, "data.NA.txt", row.names = F, col.names = T)
