Functions in R
Line 1: | Line 1: | ||
− | + | ||
In this lab the function objects were already introduced, and some basic | In this lab the function objects were already introduced, and some basic |
Revision as of 14:51, 25 January 2015
In this lab the function objects were already introduced, and some basic
functions were applied. That was the case of the c() function
which concatenates several elements in a vector, the matrix()
function which transforms a vector in a matrix, the which()
function which reports the elements where a given logical prove is TRUE,
etc..
Functions in R are very easy to apply because the programming is the same in all cases. Applying a function is as easy as to type the name of the function immediately followed (without space) by circular brackets. Inside the circular brackets, the arguments of the function are specified. A imaginary function named ResourcesAssessment() could be applied by using the following code: <<eval=FALSE>>= ResourcesAssessment(Arg_1, Arg_2, ..., Arg_i, ..., Arg_n) @ where Arg_i are the n arguments which defineing the function options.
Not all arguments must be specified to apply the function, as some arguments have default settings that are applied in the case that the argument is not specified. Some arguments requires a single element to be entered, but also vectors or even matrices, dataframes or other type of objects can be arguments in a function, as shown above. Most of the cases the argument must be entered by using its name. For instance, the argument add=TRUE controls in some graphical functions whether the current plot is produced in a new chart or incorporated to the previous one.
A question that may arise at this point is the list of the available functions in R, or even the total number of it. It is a difficult question to answer, as the number of functions is continuously increasing. There is a set of functions included with the basic R software (core), but the number of functions can be increased by installing new packages. New packages can be downloaded from the URL address of R, or being installed directly from RStudio in the "Packages" tab of the Graphical output window. Even though it is enough with installing once the new packages, packages not included in the core must be activated each time to be used in a new R session by using the library() function.
Even though the number of available functions is huge, should we be interested in create a new one. It can be easily done in R but it is far of the scope of this introductory lab.