Till this moment we were calling each built-in function and executing mostly in single lines. Now Let’s do some reals programming, which will include:
Sometimes we needs to take decisions based on condition(s).
In programming languages for doing this is called a conditional statement, and looks like this:
## [1] "not greater"
## [1] "done"
The second line of this code uses an if
statement to tell R that we want to make a choice. If the following test is true, the body of the if
(i.e., the lines in the curly braces underneath it) are executed. If the test is false, the body of the else
is executed instead. Only one or the other is ever executed:
In the example above, the test num > 100
returns the value FALSE
, which is why the code inside the if
block was skipped and the code inside the else
statement was run instead.
## [1] FALSE
And as you likely guessed, the opposite of FALSE
is TRUE
.
## [1] TRUE
Conditional statements don’t have to include an else
. If there isn’t one, R simply does nothing if the test is false:
## [1] "num is less than 100"
Exercise-1
Write a if-else condition statement to find if a number is even or odd.
Solution
## [1] "Number is even"
Exercise-2
Write a if-else condition statement to find if a number is positive.
Solution
## [1] "Positive number"
Note
The test for equality uses two equal signs,
==
. Other tests include greater than or equal to (>=
), less than or equal to (<=
), and not equal to (!=
). We can also combine tests. Two ampersands,&&
, symbolize “and”. Two vertical bars,||
, symbolize “or”.&&
is only true if both parts are true.
if (1 > 0 && -1 > 0) {
print("both parts are true")
} else {
print("at least one part is not true")
}
## [1] "at least one part is not true"
while ||
is true if either part is true:
## [1] "at least one part is true"
In this case, “either” means “either or both”, not “either one or the other but not both”.
In computer programming, a loop is a sequence of instructions that is continually repeated until a certain condition is reached.
for
loop: run some code on every value in a vectorwhile
loop: run some code while some condition is true (hardly ever used!)while
Exercise-1
Write a for loop to sum all numbers in between 1 to 10.
Solution
## [1] 55
Exercise-2
Write program to check if the input number is prime or not take input from the user
Solution
#num = as.integer(readline(prompt="Enter a number: "))
num = 7
flag = 0
# prime numbers are greater than 1
if(num > 1) {
# check for factors
flag = 1
for(i in 2:(num-1)) {
if ((num %% i) == 0) {
flag = 0
break
}
}
}
if(num == 2) flag = 1
if(flag == 1) {
print(paste(num,"is a prime number"))
} else {
print(paste(num,"is not a prime number"))
}
## [1] "7 is a prime number"
If we only had one data set to analyze, it would probably be faster to load the file into a spreadsheet and use that to plot some simple statistics. But we have twelve files to check, and may have more in the future. In this lesson, we’ll learn how to write a function so that we can repeat several operations with a single command.
Let’s start by defining a function fahrenheit_to_kelvin
that converts temperatures from Fahrenheit to Kelvin:
fahrenheit_to_kelvin <- function(temp_F) {
temp_K <- ((temp_F - 32) * (5 / 9)) + 273.15
return(temp_K)
}
We define fahrenheit_to_kelvin
by assigning it to the output of function
. The list of argument names are contained within parentheses. Next, the body of the function–the statements that are executed when it runs–is contained within curly braces ({}
). The statements in the body are indented by two spaces, which makes the code easier to read but does not affect how the code operates.
When we call the function, the values we pass to it are assigned to those variables so that we can use them inside the function. Inside the function, we use a return statement to send a result back to whoever asked for it.
Automatic Returns
In R, it is not necessary to include the return statement. R automatically returns whichever variable is on the last line of the body of the function. While in the learning phase, we will explicitly define the return statement.
Let’s try running our function. Calling our own function is no different from calling any other function:
## [1] 273.15
## [1] 373.15
We’ve successfully called the function that we defined, and we have access to the value that we returned.
Now that we’ve seen how to turn Fahrenheit into Kelvin, it’s easy to turn Kelvin into Celsius:
kelvin_to_celsius <- function(temp_K) {
temp_C <- temp_K - 273.15
return(temp_C)
}
# absolute zero in Celsius
kelvin_to_celsius(0)
## [1] -273.15
What about converting Fahrenheit to Celsius? We could write out the formula, but we don’t need to. Instead, we can compose the two functions we have already created:
fahrenheit_to_celsius <- function(temp_F) {
temp_K <- fahrenheit_to_kelvin(temp_F)
temp_C <- kelvin_to_celsius(temp_K)
return(temp_C)
}
# freezing point of water in Celsius
fahrenheit_to_celsius(32.0)
## [1] 0
This is our first taste of how larger programs are built: we define basic operations, then combine them in ever-larger chunks to get the effect we want. Real-life functions will usually be larger than the ones shown here–typically half a dozen to a few dozen lines–but they shouldn’t ever be much longer than that, or the next person who reads it won’t be able to understand what’s going on.
Nesting Functions
This example showed the output of
fahrenheit_to_kelvin
assigned totemp_K
, which is then passed tokelvin_to_celsius
to get the final result. It is also possible to perform this calculation in one line of code, by “nesting” one function inside another, like so
## [1] 0
Exercise
In the last lesson, we learned to combine elements into a vector using the c
function, e.g. x <- c("A", "B", "C")
creates a vector x
with three elements. Furthermore, we can extend that vector again using c
, e.g. y <- c(x, "D")
creates a vector y
with four elements. Write a function called highlight
that takes two vectors as arguments, called content
and wrapper
, and returns a new vector that has the wrapper vector at the beginning and end of the content:
Solution
highlight <- function(content, wrapper) {
answer <- c(wrapper, content, wrapper)
return(answer)
}
best_practice <- c("Write", "programs", "for", "people", "not", "computers")
asterisk <- "***" # R interprets a variable with a single value as a vector
# with one element.
highlight(best_practice, asterisk)
## [1] "***" "Write" "programs" "for" "people" "not"
## [7] "computers" "***"
Exercise
If the variable v
refers to a vector, then v[1]
is the vector’s first element and v[length(v)]
is its last (the function length
returns the number of elements in a vector). Write a function called edges
that returns a vector made up of just the first and last elements of its input:
Solution
edges <- function(v) {
first <- v[1]
last <- v[length(v)]
answer <- c(first, last)
return(answer)
}
dry_principle <- c("Don't", "repeat", "yourself", "or", "others")
edges(dry_principle)
## [1] "Don't" "others"
Created and Maintained by Sangram Keshari Sahu
Rmarkdown Template used from Rmdplates package
Licensed under CC-BY 4.0
Source Code At GitHub