The document discusses functions in R and their arguments. It explains that functions have formal arguments that may have default values, and arguments can be matched by position or by name. It also demonstrates using the psych package to calculate descriptive statistics and visualize the iris data grouped by species.
3. FunctionArguments
Functions have named arguments which potentially have default values.
· The formal arguments are the arguments included in the function definition
· The formals function returns a list of all the formal arguments of a function
· Not every function call in R makes use of all the formal arguments
· Function arguments can be missing or might have default values
> ?sd
> ?rnorm
> ?matrix
> ?sample
x a numeric vector or an R object which is coercible to one by
as.double(x).
na.rm logical. Should missing values be removed?
Description
This function computes the standard deviation of the values in x. If na.rm is TRUE then
missing values are removed before computation proceeds.
Usage
sd(x, na.rm = FALSE)
Arguments
4. Argument Matching
R functions arguments can be matched positionally or by name.
The following calls to sd are all equivalent
> mydata <- rnorm(100)
> sd(mydata)
> sd(x = mydata)
> sd(x = mydata, na.rm = FALSE)
> sd(na.rm = FALSE, x = mydata)
> sd(na.rm = FALSE, mydata)
5. Argument Matching
You can mix positional matching with matching by name. When an argument is matched by name, it is
“taken out” of the argument list and the remaining unnamed arguments are matched in the order that
they are listed in the function definition.
The following two calls are equivalent.
> args(lm)
function (formula, data, subset, weights, na.action,
method = "qr", model = TRUE, x = FALSE,
y = FALSE, qr = TRUE, singular.ok = TRUE,
contrasts = NULL, offset, ...)
> x = seq(1, 200, 1)
> e = rnorm(200)*50
> y = x * 5 + e
> mydata = data.frame(x, y)
> lm(data = mydata, y ~ x, model = FALSE,1:100)
> lm(y ~ x, mydata, 1:100, model = FALSE)
15. Control Structures
Control structures in R allow you to control the flow of execution of the program, depending on
runtime conditions. Common structures are
· if,else:testingacondition
· for:executealoopafixednumberoftimes
· while:executealoopwhileaconditionistrue
· repeat:executeaninfiniteloop
· break:breaktheexecutionofaloop
· next:skipaninterationofaloop
· return:exitafunction
Most control structures are not used in interactive sessions, but rather when writing functions or
longer expresisons.
16. for
for loops take an interator variable and assign it successive values from a sequence or vector. For loops
are most commonly used for iterating over the elements of an object (list, vector, etc.)
This loop takes the i variable and in each iteration of the loop gives it values 1, 2, 3, ..., 10, and then
exits.
for(i in 1:10) {
print(i)
}
17. for
These three loops have the same behavior.
x <- c("a", "b", "c", "d")
for(i in 1:4) {
print(x[i])
}
for(i in seq_along(x)) {
print(x[i])
}
for(letter in x) {
print(letter)
}
for(i in 1:4) print(x[i])
18. Nested for loops
for loops can be nested.
Be careful with nesting though. Nesting beyond 2–3 levels is often very difficult to read/understand.
x <- matrix(1:6, 2, 3)
for(i in seq_len(nrow(x))) {
for(j in seq_len(ncol(x))) {
print(x[i, j])
}
}
19. for
> print(paste("The year is", 2011))
[1] "The year is 2011"
> print(paste("The year is", 2012))
[1] "The year is 2012"
> print(paste("The year is", 2013))
[1] "The year is 2013"
> print(paste("The year is", 2014))
[1] "The year is 2014"
> print(paste("The year is", 2015))
[1] "The year is 2015”
...
...
for (i in 2010:2015){
print(paste("The year is", i))
}
for (year in c(2010,2011,2012,2013,2014,2015)){
print(paste("The year is", year))
}
20. if(<condition>) {
## do something
}
Example: if statement
x <- 5
if(x > 0){
print("Positive number")
}
Output
[1] "Positive number"
Control Structures: if
21. Control Structures: if
if...else statement
The syntax of if...else statement is:
if (test_expression) {
## do something
} else {
## do something else
}
x <- -5
if(x > 0){
print("Non-negative number")
} else {
print("Negative number")
}
Output
[1] "Negative number" 如果執⾏行的指令只有⼀一⾏行,⽐比較優雅的寫法如下:
if(x > 0) print("Non-negative number") else print("Negative number”)
x <- -5
y <- if(x > 0) 5 else 6
y
[1] 6
22. x <- 0
if (x < 0) {
print("Negative number")
} else if (x > 0) {
print("Positive number")
} else
print("Zero")
Output
[1] "Zero"
Control Structures: if
if(test_expression1) {
## do something
} else if(test_expression2){
## do something different
} else {
## do something different
}