[1] "x is negative or zero!"
A function is a block of code which only runs when it is called.
You can pass data, known as arguments, into a function.
A function can return data as a result.
Here is for example a function.
The function evaluates a statement and produces an output
Here is for example a function.
The function evaluates a statement and produces an output
This is what we had before:
Here is for example a function.
The function evaluates a statement and produces an output
This is what we had before
Here is for example a function.
The function evaluates a statement and produces an output
This is what we had before
[1] "x is negative or zero!"
This is how we write this as a function called is_this_no_positive
Here is for example a function.
The function evaluates a statement and produces an output
This is what we had before
[1] "x is negative or zero!"
This is how we write this as a function called is_this_no_positive
Here is for example a function.
The function evaluates a statement and produces an output
This is what we had before
[1] "x is negative or zero!"
This is how we write this as a function called is_this_no_positive
Here is for example a function.
The function evaluates a statement and produces an output
This is what we had before
[1] "x is negative or zero!"
This is how we write this as a function called is_this_no_positive
Here is for example a function.
The function evaluates a statement and produces an output
This is what we had before
[1] "x is negative or zero!"
This is how we write this as a function called is_this_no_positive
Here is for example a function.
The function evaluates a statement and produces an output
This is what we had before
[1] "x is negative or zero!"
This is how we write this as a function called is_this_no_positive
This is how we use this function:
This is how we use this function:
What is the output of this command?
This is how we use this function:
is_this_no_positive<-function(x) {
if (x > 0) {
message <- 'x is positive!'
} else {
message <- 'x is negative or zero!'
}
return(message)
}
is_this_no_positive(3)
[1] "x is positive!"
What is the output of this command?
This is how we use this function:
What is the output of this command?
This is how we use this function:
is_this_no_positive<-function(x) {
if (x > 0) {
message <- 'x is positive!'
} else {
message <- 'x is negative or zero!'
}
return(message)
}
is_this_no_positive(-3)
[1] "x is negative or zero!"
What is the output of this command?
Let us now create a third condition: x=0
Let us now create a third codition: x==0
Let us now create a third codition: x==0
Let us now create a third codition: x==0
Let us now create a third codition: x==0
Here is another example of a function.
We create a function called add_five
that has one parameter x
.
The function calculates and returns the sum of x
and 5
.
Here are the elements of the function:
add_five
)<-
)function
operator ( function
)(x)
){
)Here are the elements of the function:
x_plus_five <- x + 5
)return(x_plus_five)
)}
)The function add_five
, can be used to calculate the sum of various numbers and five
We can of course assign the result of a function to an object.
We can write the same function in a different way
Default arguments can be specified as part of the function definition.
For example, the following definition add_five
does not specify a default value for x
Thus, trying to call add_five
without passing an argument for x
gives an error
The following, an alternative definition, does specify the default value of 1 for x
Objectives Write an R script that classifies a given number into one of three categories using a function:
Let’s say the number is 3.
Instructions Use a function to classify the number into one of the categories. The output should look like below:
[1] "The number is Negative ."
[1] "The number is Positive ."
Objective Write an R script that classifies a person into different age groups using a function, based on the following criteria:
Let’s say the age is is 15.
Instructions
Use if-else statements to classify the age into one of the age groups within a function.
[1] "You are a Teenager."
Objective Write an R script that classifies temperatures into different categories using a function based on the following criteria:
Instructions 1. Use if-else statements within a function to classify the temperature into one of the categories.
[1] "The temperature is Moderate."
A loop is used to execute a given code section more than once.
A for
loop is composed of:
for
keywordsymbol
)in
keywordThis is the syntax in a for
loop
This is an example of a for
loop:
This is an example of a for
loop:
This is an example of a for
loop:
This is an example of a for
loop:
This is an example of a for
loop:
Here is another example with strings:
Here is another example with strings:
Here is another example with strings:
Here is another example:
Here is another example:
Here is another example:
Here is another example:
for
loop to iterate through the vector and print each element.The output should look like below
[1] "1, 1"
[1] "2, 4"
[1] "3, 9"
[1] "4, 16"
[1] "5, 25"
[1] "6, 36"
[1] "7, 49"
[1] "8, 64"
[1] "9, 81"
[1] "10, 100"
The output should look like below.
[1] "1, 1"
[1] "3, 1.73205080756888"
[1] "5, 2.23606797749979"
[1] "7, 2.64575131106459"
[1] "9, 3"
[1] "11, 3.3166247903554"
[1] "13, 3.60555127546399"
[1] "15, 3.87298334620742"
[1] 1
[1] 2
[1] 1.732051
[1] 4
[1] 2.236068
[1] 6
[1] 2.645751
[1] 8
[1] 3
[1] 10
[1] 3.316625
[1] 12
[1] 3.605551
[1] 14
[1] 3.872983
%in%
operatorThe %in%
operator returns a logical vector indicating the presence of each element of x in y
For example:
%in%
operatorThe %in%
operator returns a logical vector indicating the presence of each element of x in y
For example:
%in%
operatorThe %in%
operator returns a logical vector indicating the presence of each element of x in y
For example:
%in%
operatorThe %in%
operator returns a logical vector indicating the presence of each element of x in y
For example:
%in%
operatorThe %in%
operator returns a logical vector indicating the presence of each element of x in y
For example:
%in%
operator 1%in%
operator to identify and print the even numbers from the second vector that are also present in the first vector.[1] 36 64
%in%
operator 2Finding common elements using %in%
and a loop
The output should look like below
[1] 3 5
Popescu (JCU): Lecture 4