Monday, June 15, 2015

[5] User-Defined Functions

Sum

Create the following r source file on a certain directory.

SumF.r
SumF <- function(a,b){
  return(a+b)
}

sample code(s)
# specify the path to the .r file
sf <- "H:/Quantitative Research/R/SumF.r"
sf

# load and run the code from the file
source(sf)
SumF(1,2)
[1] 3

Mean (average)

Create the following r source file on a certain directory.

MeanF.r
MeanF <- function(a,b){
  return(mean(c(a,b)))
}

sample code(s)
# specify the path to the .r file
mf <- "H:/Quantitative Research/R/MeanF.r"
mf

# load and run the code from the file
source(mf)
MeanF(1,2)
[1] 1.5

No comments:

Post a Comment