c(1, 2, NA, 6)
Functions1: Questions
Function Arguments
Look at the help file for the function mean()
.
How many arguments does the function have?
What types of vectors are accepted?
What is the default setting for dealing with NA values?
Use the function mean()
to calculate the mean of the following values:
note the NA
and use named argument matching
Do Q2 again but rearrange the arguments.
Do Q2 again using positional matching.
Determine the class of mean()
using class()
.
Determine the class of mean()
using str()
.
Determine the class of the value output in Q4 using class()
.
Determine the class of the value output in Q4 using str().
Function environment and scoping
For each of the following sets of commands, give the value that will be returned by the last command. Try to answer without using R.
<- 5
w <- function(y) {
f return(w + y)
}f(y = 2)
<- 5
w <- function(y) {
f <- 4
w return(w + y)
}f(y = 2)
Among the variables w
, d
, and y
, which are global to f()
and which are local? What is the value of z when executing f(w)
<- 2
w <- function(y) {
f <- 3
d <- function(z) {
h return(z + d)
}return(y * h(y))
}
Do the following in R:
- Try:
<- function(a) {
myFun1 <- 3
b myFun2(a)
}
<- function(y) {
myFun2 return(y + a + b)
}
myFun1(10)
What happens?
- Now try:
<- 1
a <- 2
b myFun1(10)
What happens?