👉🏻 Perform a specific task according to a set of instructions.
👉🏻 Perform a specific task according to a set of instructions. 👉🏻 Some functions we have discussed so far,
c
,matrix
,array
,list
,data.frame
,str
,dim
,length
,nrow
,plot
👉🏻 There are basically two types of functions:
💻 Built-in functions
Already created or defined in the programming framework to make our work easier.
👨 User-defined functions
Sometimes we need to create our own functions for a specific purpose.
name <- function(arg1, aug2, ...){<FUNCTION BODY>return(value)}
cal_power <- function(x){a <- x^2; b <- x^3out <- c(a, b)names(out) <- c("squared", "cubed")out # or return(out)}
cal_power(2)
squared cubed 4 8
name <- function(arg1, aug2, ...){<FUNCTION BODY>return(value)}
cal_power <- function(x){a <- x^2; b <- x^3out <- c(a, b)names(out) <- c("squared", "cubed")out # or return(out)}
cal_power(2)
squared cubed 4 8
👉 Functions are created using the function()
1. Function name
name <- function(arg1, aug2, ...){<FUNCTION BODY>return(value)}
cal_power <- function(x){a <- x^2b <- x^3out <- c(a, b)names(out) <- c("squared", "cubed")out # or return(out)}
Function name: cal_power
Function name: cal_power
use verbs, where possible
should be meaningful
use an underscore (_) to separate words
avoid names of built-in functions
start with lower case letters. Note that R is a case sensitive language
2. Function arguments/ inputs
name <- function(arg1, aug2, ...){<FUNCTION BODY>return(value)}
cal_power <- function(x){ a <- x^2 b <- x^3 out <- c(a, b) names(out) <- c("squared", "cubed") out # or return(out)}
Function arguments: x
Function name
Function arguments/ inputs
3. Function body
name <- function(arg1, aug2, ...){<FUNCTION BODY>return(value)}
cal_power <- function(x){ a <- x^2 b <- x^3 out <- c(a, b) names(out) <- c("squared", "cubed") out # or return(out)}
Function body
cal_sqrt <- function(x){x^2}
cal_sqrt <- function(x) x^2
# Good ---if(y == 2){print("even")}# Bad ---if(y == 2){ print("even")}
Load the mozzie dataset
library(mozzie)data(mozzie); head(mozzie, 2)
ID Year Week Colombo Gampaha Kalutara Kandy Matale Nuwara Eliya Galle1 1 2008 52 15 7 1 11 4 0 02 2 2009 1 44 23 5 16 21 2 0 Hambantota Matara Jaffna Kilinochchi Mannar Vavuniya Mulative Batticalo1 6 22 0 0 8 0 0 12 5 18 1 0 0 0 0 0 Ampara Trincomalee Kurunagala Puttalam Anuradhapura Polonnaruwa Badulla1 0 0 2 1 2 0 12 1 1 10 5 0 0 1 Monaragala Ratnapura Kegalle1 1 2 162 0 1 25
Load the mozzie dataset
library(mozzie)data(mozzie); head(mozzie, 2)
ID Year Week Colombo Gampaha Kalutara Kandy Matale Nuwara Eliya Galle1 1 2008 52 15 7 1 11 4 0 02 2 2009 1 44 23 5 16 21 2 0 Hambantota Matara Jaffna Kilinochchi Mannar Vavuniya Mulative Batticalo1 6 22 0 0 8 0 0 12 5 18 1 0 0 0 0 0 Ampara Trincomalee Kurunagala Puttalam Anuradhapura Polonnaruwa Badulla1 0 0 2 1 2 0 12 1 1 10 5 0 0 1 Monaragala Ratnapura Kegalle1 1 2 162 0 1 25
Use Min-Max transformation to rescale all the districts variables onto 0-1 range.
Min-Max transformation is xi−min(x)max(x)−min(x) where x=(x1,x2,...xn).
Min-Max transformation on mozzie
minmax.colombo <- (mozzie$Colombo - min(mozzie$Colombo, na.rm = TRUE)) / (max(mozzie$Colombo, na.rm=TRUE) - min(mozzie$Colombo, na.rm=TRUE))head(minmax.colombo) # Colombo district
[1] 0.03157895 0.09263158 0.08210526 0.12000000 0.11157895 0.06105263
Min-Max transformation on mozzie
minmax.colombo <- (mozzie$Colombo - min(mozzie$Colombo, na.rm = TRUE)) / (max(mozzie$Colombo, na.rm=TRUE) - min(mozzie$Colombo, na.rm=TRUE))head(minmax.colombo) # Colombo district
[1] 0.03157895 0.09263158 0.08210526 0.12000000 0.11157895 0.06105263
minmax.gampaha <- (mozzie$Gampaha - min(mozzie$Gampaha, na.rm = TRUE)) / (max(mozzie$Gampaha, na.rm = TRUE) - min(mozzie$Gampaha, na.rm = TRUE)) head(minmax.gampaha) # Gampaha district
[1] 0.02734375 0.08984375 0.07421875 0.08984375 0.09375000 0.06640625
Min-Max transformation on mozzie
minmax.colombo <- (mozzie$Colombo - min(mozzie$Colombo, na.rm = TRUE)) / (max(mozzie$Colombo, na.rm=TRUE) - min(mozzie$Colombo, na.rm=TRUE))head(minmax.colombo) # Colombo district
[1] 0.03157895 0.09263158 0.08210526 0.12000000 0.11157895 0.06105263
minmax.gampaha <- (mozzie$Gampaha - min(mozzie$Gampaha, na.rm = TRUE)) / (max(mozzie$Gampaha, na.rm = TRUE) - min(mozzie$Gampaha, na.rm = TRUE)) head(minmax.gampaha) # Gampaha district
[1] 0.02734375 0.08984375 0.07421875 0.08984375 0.09375000 0.06640625
minmax.kalutara <- (mozzie$Gampaha - min(mozzie$Kalutara, na.rm = TRUE)) / (max(mozzie$Kalutara, na.rm = TRUE) - min(mozzie$Kalutara, na.rm = TRUE))head(minmax.kalutara) # Kalutara district
[1] 0.09333333 0.30666667 0.25333333 0.30666667 0.32000000 0.22666667
You could easily make errors.
A mistake copied becomes a mistake repeated.
You could easily make errors.
A mistake copied becomes a mistake repeated.
Whenever you need to copy and paste a block of codes many times
If you don't find a suitable built-in function to serve your purpose, you can write your own function
To share your work with others
rescale_minmax
rescale_minmax
rescale_minmax <-
rescale_minmax
rescale_minmax <-
rescale_minmax <- function(x) # Arguments/inputs should be defined inside ()
rescale_minmax <- function(x){# Task# output}
Find all the inputs that correspond to a given function output?
(x - min(x, na.rm = TRUE)) / (max(x, na.rm=TRUE) - min(x, na.rm=TRUE))
rescale_minmax <- function(x){ (x - min(x, na.rm = TRUE)) / (max(x, na.rm=TRUE) - min(x, na.rm=TRUE))}
rescale_minmax <- function(x){ (x - min(x, na.rm = TRUE)) / (max(x, na.rm=TRUE) - min(x, na.rm=TRUE))}
rescale_minmax <- function(x){ (x - min(x, na.rm = TRUE)) / (max(x, na.rm=TRUE) - min(x, na.rm=TRUE))}
rescale_minmax(c(1, 200, 250, 80, NA))
[1] 0.0000000 0.7991968 1.0000000 0.3172691 NA
Descriptive names for variables.
Comment your code.
Write a function to compute z-score value of a A/L Mathematics student given the marks of the student. Assume
mean(Mathematics) = 60, sd(Mathematics) = 10,
mean(Chemistry) = 45, sd(Chemistry) = 20,
mean(Physics) = 55, sd(Physics) = 5.
05:00
👉🏻 Perform a specific task according to a set of instructions.
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |