# Functions_in_R.R
# Introduction to R: Functions and Control
# ********************* #
# EXERCISE #
# ********************* #
# 1. Write a function to display a greeting to the user.
# The function should take as argument (userName) a string variable designating the user name (e.g. "Zina")
# The function creates the greeting "Welcome to R" followed by the value of userName (e.g. "Welcome to R Zina")
# The greeting is saved in a local variable (userGreeting)
# The functions returns the value contained in the variable userGreeting.
# In the function body, you need to:
###### a) Declare a string variable with the name userGreeting
###### b) Concatenate the Strings "Welcome to R" and userName.
###### Hint: the built-in R function 'paste' merges several strings into one.
###### e.g. s1 <- "apples"
###### s2 <- "and"
###### s3 <- "oranges."
###### > paste(s1, s2, s3)
###### [1] "apples and oranges."
###### Lookup paste in the R help by typing: ?paste
###### To exit the help, type q
###### c) Assign the concatenated strings to the variable userGreeting
# 2. Write an R function: travelDistance which calculates the distance an elastic band travels given its length and the amount by which it is stretched.
# The function takes two vector arguments: length and stretch.
# a) Open your favorite editor and use it to write the function:
# - name: travelDistance
# - arguments length and stretch
# - body: the function calculates and returns the distance traveled based on the following formula:
# distance <- stretch^2+(1/sqrt(length))
# - return: distance
# - Save your file to: distance.R . Make sure the file is saved in your working directory (i.e. '~/name').
# b) In the R console, make your function available to the R instance by sourcing your file:
# > source("distance.R")
# - Fix all errors (if any)
# c) Go back to the R console
# d) Make your function anvailable to R by sourcing it:
# > source("distance.R")
# e) Create the following vectors:
# l <- c(1.5, 2.1, 3.2, 1.7, 2.8)
# s <- c(0.3, 0.4, 0.5, 0.6, 0.7)
# f) Call your function:
# > travelDistance(l,s)
# 3. The built-in R function rnorm generates a random normal distribution, extracts a sample from it and assigns it to a vector object.
# It takes three parameters, only one of them is required: an integer value (n) designating the number of
# samples extracted from the generated distribution and assigned to the vector object.
# a) Lookup rnorm in the R help by typing: ?rnorm and study its parameters (to exit the help, just type q).
# b) Use rnorm to generate a vector of 20 samples, generated from a random normal distribution with mean 10 and standard deviation 1. Assign the vector to
# a variable with the name: nums
# c) Open your favorite text editor and write a function in R (sem) which will calculate the standard error of the mean of a vector. The function
# - Has the name: sem and takes a vector argument (call it vec) and calculates and returns its standard error of the mean. The formula is:
# sqrt(var(vec)/length(vec))
# d) Write your function to a file, load it into R, check it for errors and call the function with the nums vector you created earlier.
# e) More fun: create three vectors:
# v1 is a vector of 10 items generated from a distribution of mean 10 and standard deviation 2
# v2 is a vector of 50 items generated from a distribution of mean 10 and standard deviation 2
# v3 is a vector of 100 items generated from a distribution of mean 10 and standard deviation 2
# What happens to the SEM when the sample size is increased?
#4. The function quantile receives a numeric vector and computes sample quantiles corresponding to the given probabilities.
# The smallest observation corresponds to a probability of 0 and the largest to a probability of 1.
# a) Lookup quantile in the R help: ?quantile (to exit the help, just type q)
# b) Open your favorite text editor and create a function q.95 which computes the 95th quantile of a given vector. q.95 receives a vector object (some.vector)
# and returns the value computed by the quantile function applied to this vector and with probability 0.95
# c) Write your function to a file, load it into R and check it for errors.
# d) In the R console, create a matrix of 20 row and 20 columns with values generated from a random distribution. Give your matrix a name, e.g. my.matrix
# - Hint 1: the function rnorm(400) will generate a vector of 400 elements taken from a random distribution with mean 0 and standard deviation 1.
# You can save the vector to a variable (e.g. my.vector).
# - Hint 2: the function matrix will create a matrix. Lookup matrix in the help by typing ?natrix to determine how to specify a) the values b) number of rows c) number of columns.
# e) Use the function you defined (q.95) to obtain the 95th percentile of the rows of the matrix. Use the apply function. Save the results in a vector (e.g. q.95.rows)
# f) Plot your matrix using the matplot function. Use grey lines for plotting (lookup ?matplot for options).
# g) Hilight the 95th percentile of the row by overplotting red lines corresponding to the 20 elements of the vector q.95.rows.
# - Hint 1: use the function ?lines
# - The x-coordinates will be 1:20, corresponding to the x-axis coordinates of the matrix plot you drew earlier and the y-coordinates will be the values of q.95.rows
# Good luck!