REVISED: Saturday, March 2, 2013
"R User Defined Functions".
Everything in R is done through functions. Writing user defined functions gives you the capability of extending the basic R function library. Functions are first class objects which can be used anywhere that an R object is required. Functions can be passed as arguments to functions and returned as values from functions.
I. R FUNCTION SYNTAX
The syntax for writing a function is:
functionName <- function ( argument list ){
body
}
A. FUNCTION NAME
The function name is the first component of the function declaration.
Second, is the assignment operator <- .
C. KEYWORD FUNCTION
Third, is the keyword function which indicates to R that you want to create a function.
D. ARGUMENT LIST
Fourth, is the comma separated argument list of formal function arguments which are passed by value. A formal argument can be the special formal argument ‘...’ triple dot, a symbol, or a statement of the form ‘symbol = expression’. You can give formal arguments default values by naming the argument and assigning the argument a default value when you define the function. When, in a function declaration, an argument is followed by = and an expression, the expression sets the default value of the argument, the one which will be used unless explicitly over-ridden.
E. BODY
Fifth, is the body of the function. The body can be any valid R expression. Expressions are separated by either a semicolon ; or a new line. The body is a group of expressions contained in curly braces (‘{’ and ‘}’). A group of expressions contained within curly braces is also referred to as a block. Expressions are evaluated sequentially. Blocks are not evaluated until a new line is entered after the closing brace. The function returns the last expression executed. You want your function to only return values determined within the function itself. You do not want the return value of a function being determined by the state of the parent. Functions should be cohesive and loosely coupled. Objects in the function are local to the function; you will not be able to access them outside the function.
You can write a global variable from within a function using the <<- super assignment operator.
functionName <- function ( argument list ){
body
}
A. FUNCTION NAME
The function name is the first component of the function declaration.
B. ASSIGNMENT OPERATOR
C. KEYWORD FUNCTION
Third, is the keyword function which indicates to R that you want to create a function.
D. ARGUMENT LIST
Fourth, is the comma separated argument list of formal function arguments which are passed by value. A formal argument can be the special formal argument ‘...’ triple dot, a symbol, or a statement of the form ‘symbol = expression’. You can give formal arguments default values by naming the argument and assigning the argument a default value when you define the function. When, in a function declaration, an argument is followed by = and an expression, the expression sets the default value of the argument, the one which will be used unless explicitly over-ridden.
E. BODY
Fifth, is the body of the function. The body can be any valid R expression. Expressions are separated by either a semicolon ; or a new line. The body is a group of expressions contained in curly braces (‘{’ and ‘}’). A group of expressions contained within curly braces is also referred to as a block. Expressions are evaluated sequentially. Blocks are not evaluated until a new line is entered after the closing brace. The function returns the last expression executed. You want your function to only return values determined within the function itself. You do not want the return value of a function being determined by the state of the parent. Functions should be cohesive and loosely coupled. Objects in the function are local to the function; you will not be able to access them outside the function.
You can write a global variable from within a function using the <<- super assignment operator.
II. R FUNCTION DIRECTIVE
Type the function's name without the ( ) to view a function's code.
A. function( ) DIRECTIVE
Functions are created using the function( ) directive, and are R objects, of class function.
Functions can be passed as arguments to other functions. Functions can be nested within functions. The last expression in the function body to be evaluated is the return value of the function. If you do not want a R console print to occur you can use the invisible( ) function and it will not be printed. Functions have named arguments, and the arguments can have default values.
B. THE ... ARGUMENT
IV. R NUMERIC FUNCTIONS
FUNCTION DESCRIPTION
abs(x) absolute value
sqrt(x) square root
ceiling(x) ceiling(3.475) is 4
floor(x) floor(3.475) is 3
trunc(x) trunc(5.99) is 5
round(x, digits=n) round(3.475, digits=2) is 3.48
signif(x, digits=n) signif(3.475, digits=2) is 3.5
cos(x), sin(x), tan(x) also acos(x), cosh(x), acosh(x), etc.
log(x) natural logarithm
log10(x) common logarithm
exp(x) e^x
V. R LOGICAL OPERATIONS
< (less than) and <= (less than or equal to)
> (greater than) and >= (greater than or equal to)
== (equal to) and != (not equal to)
& ("and")
| ("or")
! ("not")
VI. R FUNCTION EXAMPLES
A. CHANGE R STUDIO WORKING DIRECTORY
changecwd <- function(cwd)
{
setwd("C:/Previous/WD/cwd")
}
VII. REFERENCES
Type the function's name without the ( ) to view a function's code.
A. function( ) DIRECTIVE
Functions are created using the function( ) directive, and are R objects, of class function.
Functions can be passed as arguments to other functions. Functions can be nested within functions. The last expression in the function body to be evaluated is the return value of the function. If you do not want a R console print to occur you can use the invisible( ) function and it will not be printed. Functions have named arguments, and the arguments can have default values.
The "formal arguments" are the arguments included in the function definition. The formals( ) function returns a list of all the formal arguments in a function.
Not every function call in R makes use of all the formal arguments. Function arguments might be missing or have default values. R function arguments can be matched positionally or by name. You can mix matching by name with positional matching. When an argument is matched by name it is taken out of the argument list and the remaining unnamed arguments are matched in the order they appear in the function definition. You can also set an argument value to NULL, in addition to not specifying a default value. Function arguments are evaluated "lazily", which means they are only evaluated as needed.
B. THE ... ARGUMENT
The argument "..." indicates a variable number of arguments that are usually passed on to other functions. "..." is usually used when extending another function and you do not want to copy the entire argument list of the original function. Generic functions use ... so that extra arguments can be passed to methods. The "..." is also necessary when the number of arguments passed to the function cannot be known in advance. One catch with "..." is that any arguments which appear after "..." on the argument list must be named explicitly and can not be partially matched.
III. CALLING R FUNCTIONS
Function invocation is also referred to as calling R functions. R functions are called by name, with a list of arguments, separated by commas.
Function invocation is also referred to as calling R functions. R functions are called by name, with a list of arguments, separated by commas.
FUNCTION DESCRIPTION
abs(x) absolute value
sqrt(x) square root
ceiling(x) ceiling(3.475) is 4
floor(x) floor(3.475) is 3
trunc(x) trunc(5.99) is 5
round(x, digits=n) round(3.475, digits=2) is 3.48
signif(x, digits=n) signif(3.475, digits=2) is 3.5
cos(x), sin(x), tan(x) also acos(x), cosh(x), acosh(x), etc.
log(x) natural logarithm
log10(x) common logarithm
exp(x) e^x
V. R LOGICAL OPERATIONS
> (greater than) and >= (greater than or equal to)
== (equal to) and != (not equal to)
& ("and")
| ("or")
! ("not")
VI. R FUNCTION EXAMPLES
A. CHANGE R STUDIO WORKING DIRECTORY
{
setwd("C:/Previous/WD/cwd")
}
VII. REFERENCES
Enjoy R User Defined Functions.
Elcric Otto Circle
-->
-->
-->
How to Link to My Home Page
It will appear on your website as:"Link to: ELCRIC OTTO CIRCLE's Home Page"
No comments:
Post a Comment