Extended Syllabus PDF
PDF - (179 - 185)
A function is a set of statements organized together to perform a specific task
ex: mean() (arithmetic mean)
## [1] 2
## [1] 2
Problem: Take a sample belonged to population, and sum
## [1] 1 2 3 4 5 6
## [1] 3 1
## [1] 4
I want to collect my sample() and sum() functions and put in ONE function. I will create a new function named roll2().
name_new_function <- function( argument ) {
name_new_variable <- do_this( argument, option )
then_do_this( name_new_variable )
}
name_new_function( ) # It will work with my default argument
name_new_function <- function( argument ) {
name_new_variable <- do_this( argument, option )
then_do_this( name_new_variable )
}
name_new_function( ) # It will work with my default argument
in formal :
## [1] 9
## [1] 7
You can change the default argument in every time
## [1] 17
## [1] 14
## [1] 9.5
You can add new options or new functions in your new function.
{ } and () are important
# Think about these functions
# mean(), print(), plot(), max(), install.packages(), help(), ...
TRUE & TRUE
TRUE & FALSE
TRUE | FALSE
!TRUE
2 == 3
5 < 6
c(1,4) >= 6
9 != 8
5 < 6 & 9 != 8
score <- 80
exam_name <- "math"
score >= 75 & exam_name == "math"
Check, Conditions and Loops, DataCamp