Functions in R

From AWF-Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "\subsection{Functions in \textsf{R}} In this lab the function objects were already introduced, and some basic functions were applied. That was the case of the {\tt c()} funct...")
 
Line 1: Line 1:
\subsection{Functions in \textsf{R}}
+
==Functions in '''R'''==
 +
 
 
In this lab the function objects were already introduced, and some basic  
 
In this lab the function objects were already introduced, and some basic  
functions were applied. That was the case of the {\tt c()} function  
+
functions were applied. That was the case of the ''c()'' function  
which concatenates several elements in a vector, the {\tt matrix()}
+
which concatenates several elements in a vector, the ''matrix()''
function which transforms a vector in a matrix, the {\tt which()}
+
function which transforms a vector in a matrix, the ''which()''
 
function which reports the elements where a given logical prove is TRUE,  
 
function which reports the elements where a given logical prove is TRUE,  
 
etc..
 
etc..
  
Functions in \textsf{R} are very easy to apply because the programming  
+
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  
 
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  
 
name of the function immediately followed (without space) by circular  
 
brackets. Inside the circular brackets, the arguments of the function  
 
brackets. Inside the circular brackets, the arguments of the function  
are specified. A imaginary function named {\tt ResourcesAssessment()}
+
are specified. A imaginary function named ''ResourcesAssessment()''
 
could be applied by using the following code:
 
could be applied by using the following code:
 
<<eval=FALSE>>=
 
<<eval=FALSE>>=
 
ResourcesAssessment(Arg_1, Arg_2, ..., Arg_i, ..., Arg_n)
 
ResourcesAssessment(Arg_1, Arg_2, ..., Arg_i, ..., Arg_n)
 
@
 
@
where {\tt Arg\_i} are the n arguments which defineing the function  
+
where ''Arg_i'' are the n arguments which defineing the function  
 
options.
 
options.
  
Line 25: Line 26:
 
of objects can be arguments in a function, as shown above. Most of the  
 
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  
 
cases the argument must be entered by using its name. For instance, the  
argument {\tt add=TRUE} controls in some graphical functions whether 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.
 
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  
 
A question that may arise at this point is the list of the available  
functions in \textsf{R}, or even the total number of it. It is a  
+
functions in '''R''', or even the total number of it. It is a  
 
difficult question to answer, as the number of functions is continuously  
 
difficult question to answer, as the number of functions is continuously  
 
increasing. There is a set of functions included with the basic  
 
increasing. There is a set of functions included with the basic  
\textsf{R} software (core), but the number of functions can be increased  
+
'''R''' software (core), but the number of functions can be increased  
 
by installing new packages. New packages can be downloaded from the URL  
 
by installing new packages. New packages can be downloaded from the URL  
address of \textsf{R}, or being installed directly from \textsf{RStudio}
+
address of '''R''', or being installed directly from '''RStudio'''
 
in the "Packages" tab of the Graphical output window. Even though it is  
 
in the "Packages" tab of the Graphical output window. Even though it is  
 
enough with installing once the new packages, packages not included in  
 
enough with installing once the new packages, packages not included in  
the core must be activated each time to be used in a new \textsf{R}
+
the core must be activated each time to be used in a new '''R'''
session by using the {\tt library()} function.
+
session by using the ''library()'' function.
  
 
Even though the number of available functions is huge, should we be  
 
Even though the number of available functions is huge, should we be  
interested in create a new one. It can be easily done in \textsf{R} but  
+
interested in create a new one. It can be easily done in '''R''' but  
 
it is far of the scope of this introductory lab.
 
it is far of the scope of this introductory lab.

Revision as of 15:50, 25 January 2015

Functions in R

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.

Personal tools
Namespaces

Variants
Actions
Navigation
Development
Toolbox
Print/export