[R] specifying arguments in functions and calling functions, within functions

K. Brand k.brand at erasmusmc.nl
Wed Sep 26 17:56:07 CEST 2012


Esteemed R UseRs,

Regarding specifying arguments in functions and calling functions
within functions:

## beginning ##
## some data
ex <- rnorm(10)
ex[5] <- NA

## example function
Scale <- function(x, method=c("mean", "median")) {
   scl <- method
   scl(x)
}

## both return NA
Scale(ex, method=median)
median(ex, na.rm=FALSE)

## both return the median
Scale(ex, method="median")
median(ex, na.rm=TRUE)

## 1. Why does the use of apostrophes have this effect when calling
## a fucntion within a function?

## 2. What's the canonical use of apostrophes in functions like the above:
## Scale <- function(x, method=c("mean", "median")) {....
## or
## Scale <- function(x, method=c(mean, median)) {....

## 3. How does one specify the arguments of a function being called
## within a function? i.e. i thought the use of '...' might work in
## the following but i was wrong.

## '...' has no apparent effect
Scale <- function(x, method=c("mean", "median"),...) {
   scl <- method
   scl(x)
}

## both return NA
Scale(ex, method=median, na.rm=TRUE)
Scale(ex, method=median, na.rm=FALSE)
## end ##


I failed to comprehend anything google returned when trying to
understand this myself. Greatly appreciate any thoughts &/or
examples on this.

Tia, Karl

-- 
Karl Brand
Dept of Cardiology and Dept of Bioinformatics
Erasmus MC
Dr Molewaterplein 50
3015 GE Rotterdam
T +31 (0)10 703 2460 |M +31 (0)642 777 268 |F +31 (0)10 704 4161


More information about the R-help mailing list