The Big R-Book. Philippe J. S. De Brouwer

Чтение книги онлайн.

Читать онлайн книгу The Big R-Book - Philippe J. S. De Brouwer страница 38

The Big R-Book - Philippe J. S. De Brouwer

Скачать книгу

      image Further information – Speed

      Further information about optimising code for speed and more elegant and robust timing of code can be found in Chapter 40The Need for Speed” on page 997.

       functions

      The user is both able to use the built-in functions and/or define his own bespoke functions.

      4.6.1 Built-in Functions

      Right after starting, R some functions are available. We call these the “built-in functions.” Some examples are:

       demo(): shows some of the capabilities of R

       demo()

       q(): quits R

       q()

       data(): shows the datasets available

       data()

       help(): shows help

       help()

       ls(): shows variables

       ls()

       c(): creates a vector

       c()

       seq(): creates a sequence

       seq()

       mean(): calculates the mean

       mean()

       max(): returns the maximum

       max()

       sum(): returns the sum

       sum()

       paste(): concatenates vector elements

       paste()

      4.6.2 Help with Functions

      If you do not remember exactly what parameters a certain function needs or what type of variable the output will be, then there are multiple ways to get support in R.

       apropos()

      Help with functions

      help(c) # shows help help with the function c ?c # same result apropos(“cov”) # fuzzy search for functions

      image Further information on packages

      R has many “packages” that act as a library of functions that can be loaded with one command. For more information refer to Chapter 4.7 “Packages” on page 96.

      4.6.3 User-defined Functions

       function()

       function – create

      Function use for function()

      In R a user defined function (UDF) is created via the function function().

      function_name <- function(arg_1, arg_2, …) { function_body return_value }

      Example: A bespoke function

      # c_surface # Calculates the surface of a circle # Arguments: # radius -- numeric, the radius of the circle # Returns # the surface of the cicle c_surface <- function(radius) { x <- radius 2 * pi return (x) } c_surface(2) + 2 ## [1] 14.56637

      Note that it is not necessary to explicitly “return” something. A function will automatically return the last value that is send to the standard output. So, the following fragment would do exactly the same:

      # c_surface # Calculates the surface of a circle # Arguments: # radius -- numeric, the radius of the circle # Returns # the surface of the cicle c_surface <- function(radius) { radius 2 * pi } # Test the function: c_surface(2) + 2 ## [1] 14.56637

      4.6.4 Changing Functions

      Usually,we will keep functions in a separate file that is then loaded in our code with the command source(). Editing a function is then done by changing this file and reloading it – and hence overwriting the existing function content.

       edit()

       fix()

      # Edit the function with vi: fix(c_surface) # Or us edit: c_surface

Скачать книгу