################################################################################
### R BASICS WORKSHOP ###
### EXERCISE 2.1: Functions and Arguments ###
### ###
### Center for Conservation and Sustainable Development ###
### Missouri Botanical Garden ###
### Website: rbasicsworkshop.weebly.com ###
################################################################################
## OBJECTIVE:
## The objective of this exercise is to practice the concepts of 'function' and
## 'argument', and learn some basic functions in R.
rep(x="your name", times=10)
# This repeats "your name" 10 times
## TASK 1. Practice writing the same like of code witout argument names.
## YOUR ANSWER HERE:
## TASK 2. Practice writing the same like of code, changing the order of the
## arguments.
## YOUR ANSWER HERE:
## TASK 3. Replace the text "your name" with something different, and re-write
## the function so that it repeats it 10,000 times.
## YOUR ANSWER HERE:
rep(x=c("your name", "friend's name"), times=10)
# This comand repeats "your name" and "friend's name" 10 times
## TASK 4. How many functions do you have in this command? Which are they?
## YOUR ANSWER HERE:
## TAKS 5. Re-write the command so that "your name" is repeated 10 times BEFORE
## "friend's name" is repeated also 10 times. HINT: use function to lear about
## the argument *each* of the function *rep*.
## YOUR ANSWER HERE:
## TASK 6. Re-write this comend to repeat "your name" 10 times, and then repeat
## "friend's name" only 6 times.
## YOUR ANSWER HERE:
## TASK 7. How many times did you have to use a function in task 6?
## YOUR ANSWER HERE:
# There a number of pre-installed datasets in R. The function *data* loads those
# datasets.
data("cars")
# This command loads the data set "cars" that has information on car speeds and
# how much distance it takes for it to stop.
help("cars")
# The function *help* can also be used to consult information on datasets.
## TASK 8. What is the name of the argument in the function "help" that I am
## omiting and which is taking the value "cars"?
## YOUR ANSWER HERE:
attach(cars)
# The function *attach* makes the variables in the dataset "cars" easy to access
plot(speed, dist)
# The function *plot* is a generic function that makes many types of graphics,
# but most commonly scatterplots.
## TASK 9. Re-write the command above to use names of arguments explicitly.
## YOUR ANSWER HERE:
## TASK 10. In the command above, use the arguments: *col* to change the color
## of the symbols (e.g. col="red"). You can find a partial list of R colors in
## R among the resources in the workshop website.
## YOUR ANSWER HERE:
## TASK 11. Now, in addition to changing the color, use the arguments "xlab"
## and "ylab" to re-name the x-axis and y-axis.
## YOUR ANSWER HERE:
lm(dist~speed)
## TASK 12. What does this line of code do? (what does function *lm* do?)
## YOUR ANSWER HERE:
summary(lm(dist~speed))
## TASK 13. What does this line of code do?
## YOUR ANSWER HERE:
plot(speed, dist, pch=16, col="grey60", xlab="Car Speed",
ylab="Stopping Distance")
abline(lm(dist~speed), lwd=2, col="black")
# Here, we first create the plot with the function *plot. Then, we
# use the function *abline* to add a regression line to the plot.
plot(rnorm(100, 3, 2), rpois(100, runif(1)), pch=16, col="lightblue",
xlab="Random Values Normal Distri.", ylab="Random Values Poisson Distri.")
## TASK 14. How many functions are used in this command?
## YOUR ANSWER HERE:
## TASK 15. How many arguments are passed to the function *plot*?
## YOUR ANSWER HERE:
## TASK 16. What does this line of code do?
# Step 1: Generate 100 values at random from a normal distribution with
# mean 3 and standard deviation 2
# Step 2:
# Step 3:
# ...
## YOUR ANSWER HERE:
################################################################################
### TASK SOLUTIONS #############################################################
################################################################################
## TASK 1.
rep("your name", 10)
## TASK 2.
rep(times=10, x="your name")
## TASK 3.
rep(times=10, x="Juan")
## TASK 4.
# 2 function, rep and c
## TAKS 5.
rep(x=c("Sebas", "Ivan"), time=10)
## TASK 6.
rep(x=c("Sebas", "Ivan"), time=c(10,6))
## TASK 7.
# 3 times, *rep* once and *c* twice
## TASK 8.
# The name is *topic*: help(topic="cars")
## TASK 9.
plot(x=speed, y=dist)
## TASK 10.
plot(x=speed, y=dist, col="forestgreen")
## TASK 11.
plot(x=speed, y=dist, col="forestgreen", xlab="Car Speed",
ylab="Stopping Distance")
## TASK 12.
# The function "lm" fits models like linear regressions.
## TASK 13.
# This creates a summary of the results of a linear regression between dist and
# speed
## TASK 14. How many functions are used in this command?
# 4 functions are used: plot, rnorm, rpois, and runif.
## TASK 15. How many arguments are passed to the function *plot*?
## YOUR ANSWER HERE:
# 6 arguments are passed.
## TASK 16. What does this line of code do?
# Step 1: Generate 100 values at random from a normal distribution with
# mean 3 and standard deviation 2
# Step 2: Generate 1 value from a uniform distribution between 0 and 1.
# Step 3: Generate 100 random values from a Poisson distribuiton, using the
# random value of step 2 to define the lambda argument.
# Step 4. Plot the first set of random values agains the second set.