(Sections 4.1 - 4.2)
readline()
Prompts the User## What is your name? Cowardly Lion
## Hello, Cowardly Lion!
The input is treated as a character vector, so to use it as a number you should convert it first.
## Give me a number, and I'll add 10 to it: 15
## The sum is: 25
Conditional statements allow R to make decisions that depend upon various conditions.
Let’s design a simple guessing-game for the user:
if
The condition
is evaluated as a Boolean expression.
sample()
The sample()
function randomly picks a value from the vector that is is given.
size
parameter specifies how many numbers to pick.sample()
if ... else
Spell out what will happen if the condition is false:
if-else
Three positive numbers \(a\), \(b\) and \(c\) form the sides of a right triangle if and only if:
\[a^2 + b^2 = c^2.\]
Write a program that prompts the user for three positive numbers \(a\), \(b\) and \(c\), and then tells the user whether or not they form a right triangle. It should work like this:
> triCheck() > triCheck()
Enter side a: 3 Enter side a: 3
Enter side b: 4 Enter side b: 4
Enter side c: 5 Enter side c: 6
Right triangle! Not a right triangle.
Try it out:
The next example will use R’s function for absolute value:
Use abs()
with subtraction to find the “distance” between two numbers. For example, 5 and 30 are 25 units apart.
Another example: checking whether or not two number a one unit apart:
if ... else
’snumber <- sample(1:4, size = 1)
guess <- as.numeric(readline("Guess the number (1-4): "))
if ( guess == number ) {
cat("Congratulations! You are correct.")
} else if ( abs(guess - number) == 1 ){
cat("You were close!\n")
cat("The correct number was ", number, ".\n", sep = "")
} else {
cat("You were way off.\n")
cat("The correct number was ", number, ".\n", sep = "")
}
ifelse()
FunctionHere are some heights:
Suppose that you have a lot of heights:
You would like to classify each person as either “tall” or “short”, depending on whether they are respectively more or less than 71 inches in height.
ifelse()
[1] "short" "short" "short" "tall" "short" "short" "tall" "short"
ifelse()
takes three parameters:
test
: the condition you want to evaluate;yes
: the value that gets assigned when test
is true;no
: the value assigned when test
is false;Most people don’t name them:
If a triangle has three sides of length \(x\), \(y\) and \(z\), then the sum of any two sides must be greater than the remaining side:
\[\begin{aligned} x + y &> z, \\ x + z &> y, \\ y + z &> x. \end{aligned}\]
This function decides whether three given sides can form a triangle:
Try it out:
Suppose you have six sets of sides:
\[(2,4,5),(4.7,1,3.8),(5.2,8,12),\\ (6, 6, 13), (6, 6, 11), (9, 3.5, 6.2)\]
Which of them can make a triangle?
Enter the sides into three vectors:
The first elements of a
, b
and c
make the first set of sides, and so on …
Decide about them all at once:
Then we can decide about all six triples at once:
Vectorization is at work here!
Use ifelse()
to classify them all at once:
Earlier you wrote a program that tells whether or not three given line segments form a right triangle. Now write a function called rightTriangle()
that will decide, for any number of sets of three sides, whether or not the sides form a right triangle. It should work like this:
Here is one way: