Kansas
Kansas
Kansas
Kansas
(Sections 3.1 - 3.2)
Dorothy wants to say “Kansas” four times. This is easy enough:
Here’s a way to do it in just two lines:
Don’t Repeat Yourself (DRY)
A principle of computer programming that holds that general solutions should be set forth in one place and usable in many places, and that information needed in many places should be defined authoritatively in one place.
Writing functions keeps your code DRY.
manyCat
manyCat(word = "Kentucky", n = 5)
function
is the reserved word in R that permits us to define functions.
The general form of a function definition is:
Parameters of a Function
The parameters of a function (also called the formal arguments of the function) are the names that will be used in the body of the function to refer to the actual arguments supplied to the function when it is called.
Argument
An argument for a function is a value that is assigned to one of the parameters of the function.
Parameter | Argument |
---|---|
word | “Kansas” |
n | 4 |
Body of a Function
The body of a function is the code that is executed when the function is called.
In R, when the body consists of more than one expression then it appears inside of curly braces.
… if the body is just one expression:
… as long as you keep in mind the order.
These calls all do the same thing:
## NAs introduced by coercion
## Error in rep(wordWithNewline, times = n) :
## invalid 'times' argument
R thought:
word
is 4n
is “Hello”What would happen if you enter this?
Why?
DON’T:
DO:
Break your work into five steps:
Write a function called scramble()
that takes a given vector and returns a vector consisting of all the odd-numbered elements in front of all of the even-numbered elements.
The function should tke a single parameter called vec
, the vector to be scrambled.
Typical examples of use should be like this:
Read the specifications carefully.
So let’s go back and read them again!
Write a program that performs the stated task, for a specific example. If necessary, revise your program until it is fully general.
Set up an example:
Pro Tip: Your example should have the same name as the desired parameter. (That will make later work easier.)
What are the odd indices of vec
?
They are 1, 3 and 5.
What are the even indices of vec?
They are 2, 4, and 6.
So we want this:
“If necessary, revise your program until it is fully general.”
Here is our program:
[1] "the" "is" "my" "cat" "in" "chair"
It will be “fully general” if the only thing we have to change is the binding to vec
.
[1] "the" "is" "my" "cat" "in" "elm"
Just changing vec
to the desired input was not enough!
Our solution won’t work unless vec
has length 6.
We need a program that works for a vector of any length.
Let’s make a name for the length:
Construct the odd indices:
Construct the even indices:
All of the indices:
Now select, based on all of the indices:
So it seems this program will work:
vec <- c("the", "cat", "is", "in", "my", "chair")
n <- length(vec)
oddIndices <- seq(1, n, by = 2)
evenIndices <- seq(2, n, by = 2)
allIndices <- c(oddIndices, evenIndices)
vec[allIndices]
[1] "the" "is" "my" "cat" "in" "chair"
If the program REALLY works, it will work for a different example just by changing vec
.
So let’s change vec
and see:
vec <- letters
n <- length(vec)
oddIndices <- seq(1, n, by = 2)
evenIndices <- seq(2, n, by = 2)
allIndices <- c(oddIndices, evenIndices)
vec[allIndices]
[1] "a" "c" "e" "g" "i" "k" "m" "o" "q" "s" "u" "w" "y" "b" "d" "f" "h" "j" "l"
[20] "n" "p" "r" "t" "v" "x" "z"
Looking good!
Encapsulate your work into a function.
The function-skeleton is this:
You encapsulate all of the “work” (except the first line that defined vec
).
Test the function on a few examples.
Read the specifications again. If necessary, revise the code for the function until it meets all specifications, and test again.
OK, let’s do that …
Write a function called scramble()
that takes a given vector and returns a vector consisting of all the odd-numbered elements in front of all of the even-numbered elements.
The function should tke a single parameter called vec
, the vector to be scrambled.
Typical examples of use should be like this:
… It seems that we really met all of the problem specifications. We are done!
Why is the above an incorrect solution to the problem?
scramble()
, not scrambleWrong()
.