Functions in R
Functions in R
Functions
Topic & Structure of the lesson
-Writing functions
- How to call a function
- Named Arguments
- Default values for Arguments
- Return value from Functions
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/O
Functions SlideSlide
2 of 255of 12
Learning outcomes
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/O
Functions SlideSlide
3 of 355of 12
Key terms you must be able to use
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/O
Functions SlideSlide
4 of 455of 12
Functions
- Functions are used to logically break our code
into simpler parts which become easy to maintain
and understand.
- It’s pretty straightforward to create your own
function in R programming.
- Syntax for Writing Functions in R
func_name <- function (argument) {
statement
}
-Here we can see that the reserved word function is used to declare
a function in R
-The statements within the curly braces form the body of the
function.
- func_name-> name assigned to a function
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/O
Functions SlideSlide
5 of 555of 12
Example of a Function
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/O
Functions SlideSlide
6 of 655of 12
Default Values for Arguments
-We can assign default values to arguments in a function in R.
- This is done by providing an appropriate value to the formal argument
in the function declaration.
- Here is the above function with a default value for y
Eg:
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/O
Functions SlideSlide
7 of 755of 12
R Return Value from Function
-Functions are generally used for computing some value, so they need
a mechanism to supply that value back to the caller
- This is called returning.
- The value of the last line of the code in a function is automatically
returned.
- The return command specifies that a value should be returned and
the function should be exited
- The syntax of return is:
return(expression)
Eg: check <- function(x) {
if (x > 0) {
result <- "Positive"
}else if (x < 0) {
result <- "Negative"
}else {
result <- "Zero"
}
return(result)
}
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/O
Functions SlideSlide
8 of 855of 12
Quick Review Questions
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/O
Functions SlideSlide
9 of 955of 12
Summary of Main Teaching Points
• Functions
• Create function
• Passing arguments
• Return value
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/O
Functions SlideSlide
10 of1055of 12
Q&A
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/O
Functions SlideSlide
11 of1155of 12
Next Session
• Data Exploration
-Reading data into R
- Read from Excel sheet
- Read from Table
CT038-3-2 Object
CT127-3-2 Oriented Development
Programming with Java
for Data Analysis File I/O
Functions SlideSlide
12 of1255of 12