### Practicals 1

# 1
number8<-5+3 #sums 5 and 3 and saves the result into object number8

number8 # displays the content of the number8 object
# [1] 8

print(number8)# a more explicit way to displaying the conmtent of an object
# [1] 8

# 2 
vec.1<-c(1, 2,5,4,8,10.8) #Creates vector containing 6 numbers

# 3 
vec.1[4] #Displays the 4th value of vec.1
# [1] 4

# 4
vec.1[5]<-NA

# 5
getwd()#displays working directory

# 6
?boxplot # displays help for the function boxplot

# 7
install.packages("readxl")# installs the package readxl
library(readxl)# loads the package readxl

people<-read_excel("people.xlsx")# imports the people dataset from excel file into a tibble called people 

# 8
print(people)# displays a shortened and tibble content with info on variable types
summary(people)#displays the summary

# 9
class(people)# shows object type of people
# [1] "tbl_df"     "tbl"        "data.frame"

class(people$eye.color)# shows the type of the eye.color variable
# [1] "character"

class(people$height)
# [1] "numeric"

# 10
quantile(people$height)# computes the quantiles of height values
#    0%   25%   50%   75%  100% 
# 160.0 174.5 180.0 185.0 200.0 

#0% = minimum
#25% = lower quartile
#50% = median
#75% = upper quartile
#100% = maximum

# 11
males<-people[people$sex=="M",]# Create a new tibble containing just data on males

# 12
aggregate(people$height, list(people$sex), median)#computes aggregated meadian height values for males and females
tapply(people$height, list(people$sex), median)# the same with different output format

# 13
hist(people$height)#Plots a histogram of heights

# 14 
boxplot(height~sex, data=people)#Plots a boxplot showing association between sex and heights

# 15
#Creating a png file that can be imported to Word

png("people.png", width = 1200, height=1200, res=300)#Creating and opening the file for plotting
# width and height are given in pixels, res is dpi. The resulting picture is thus 4 x 4 inches ~ 10 x 10 cm
boxplot(height~sex, data=people) # Plotting
dev.off() # Closing and saving the file. This is crucial!

# Alternatively, you can use window-based export which however produces low resolution pictures 

