(Section 9.3)
seat:Here’s a slow, annoying way:
split()Here’s a cool way:
This won’t work to get at one of the frames (try it):
1_front is not a legal name for a variable. (Cannot begin with a number!)
You can do this:
Or this:
Recall the flowers in the meadow:
Our goal: store results of all walks in a list.
walk_meadow_vec <- function(color, wanted) {
picking <- TRUE
## the following will be extended to hold the flowers picked:
flowers_picked <- character()
desired_count <- 0
while (picking) {
picked <- sample(flower_colors, size = 1)
flowers_picked <- c(flowers_picked, picked)
if (picked == color) desired_count <- desired_count + 1
if (desired_count == wanted) picking <- FALSE
}
## return the vector of flowers picked:
flowers_picked
}all_walk_list <- function(people, favs, numbers) {
## initialize a list of the required length:
lst <- vector(mode = "list", length = length(people))
for (i in 1:length(people)) {
fav <- favs[i]
number <- numbers[i]
lst[[i]] <- walk_meadow_vec(
color = fav,
wanted = number
)
}
## give names:
names(lst) <- people
## return the list
lst
}nchar() returns the number of characters in a string:
toupper() makes all the letters of a string upper-case:
This function takes two strings and returns the number of characters in each:
This function takes two strings and returns the upper-case versions of each:
We would like a function that takes a string and returns:
"7" is a string, not a number!
We only get the string!
We only get the number!
Return a list!
What is
...?
... stands for “any other arguments you want to add”:
Remember the na.rm parameter of functions like mean():
na.rm = TRUE[1] 3.0 7.5
But this decides the option for the user! Can we let the user decide?