R: functions

Defining a function in R, looks like

 mynewsumfunction <- function(x,y = 34){
  x + y
 }

Parameters can have a default value.
If parameters are strings  you can use function paste

paste(c("The", "quick", "brown", "fox"))
[1] "The"  "quick" "brown" "fox"

Ellipsis allows to declare a variable number of parameters in a function.

> myhist <- function(x, border="blue", ...){
+ 	hist(x, border=border, ...)
+ }
> set.seed(5)
> myhist(rnorm(1000), breaks=30)
> set.seed(5)
> myhist(rnorm(1000))  

Function prototypes can be checked with args function:

args(mean)
function (x, ...)
Environments:

Every function and symbol has to be defined in an environment:

  • global
  • packages loaded
  • package base
search (myfunction)

Functions can be defined within other functions.

environment(myfunction)

Autor: tres

http://www.linkedin.com/pub/ester-niclos-ferreras/9/887/3b4

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *