04 – Functions
Sebastian Raschka
6/17/2020
Source file: https://github.com/rasbt/R-notes/blob/master/04-functions.Rmd
Functions
- Note that in contrast to Python, R doesn’t require an explicit
returncall – ifreturnis not used, the R function will return the output of its last expression. For instance, the following two code snippets
mean_squared_error <- function(x, y){
mean_diff <- mean(x - y)
mean_diff^2
}
mean_squared_error(c(1, 2, 3), c(1, 2, 5))
## [1] 0.4444444
and
mean_squared_error <- function(x, y){
mean_diff <- mean(x - y)
return(mean_diff^2)
}
mean_squared_error(c(1, 2, 3), c(1, 2, 5))
## [1] 0.4444444
both return 0.4444444
- For your convenience, you can save functions in a separate file and load it into your current R session. E.g., you can save the
mean_squared_errorwe created above as a .R file, for example,mse.R. Then, you can source the file in your current R session to load the function, a shown in the screenshot below:
Importing Code
source()
Developing R Packages
- If you are interested in developing your own R packages (also known as “extensions”), have a look at the official guide available at https://cran.r-project.org/doc/manuals/r-release/R-exts.html.