Using functions with several parameters

A function in R can have more than one parameter. In this section, we are going to get acquainted with supplying several arguments to such functions. At the same time, several new functions that take more than one argument will be introduced.

Supplying more than one argument in a function call

When specifying several arguments in a function, we need to assign each argument to the respective parameter using the usual assignment operator = during the function call, separating the assignment expressions for different parameters with commas.

For example, let's examine the seq function. Its most useful three parameters are from, to, and by (you can see in the function's help page that it has several more parameters). The seq function creates a sequential vector based on the input, as follows:

  • from: This parameter specifies from where to begin
  • to: This parameter specifies where to end
  • by: This parameter specifies the step size

Let's take a look at the following examples:

> seq(from = 100, to = 150, by = 10)
[1] 100 110 120 130 140 150
> seq(from = 190, to = 150, by = -10)
[1] 190 180 170 160 150

Note

The : operator we previously encountered is, in fact, used to create sequences of special cases (where the step size is 1 or -1), while the seq function is more general.

There are several important rules regarding function calls involving more than one argument:

  • The names of the parameters can be omitted as long as the arguments are entered in the default order, which is specified in the function definition. Therefore, the following two expressions are equivalent:
    > seq(from = 5, to = 10, by = 1)
    [1]  5  6  7  8  9 10
    > seq(5, 10, 1)
    [1]  5  6  7  8  9 10

    We have, in fact, used this property already. For example, the name of the first argument of the mean function (the vector to compute the mean for) is x, but we can omit it during the function call:

    > mean(1:10)
    [1] 5.5
    > mean(x = 1:10)
    [1] 5.5
  • On the contrary, if the parameter names are specified, the arguments order can be altered:
    > seq(to = 10, by = 1, from = 5)
    [1]  5  6  7  8  9 10
  • Arguments can be skipped as long as they have a default value in the function definition. For example, the by parameter has the default argument of 1, therefore the following two expressions are equivalent:
    > seq(5, 10, 1)
    [1]  5  6  7  8  9 10
    > seq(5, 10)
    [1]  5  6  7  8  9 10

Creating default vectors

New vectors populated with default values (0 for numeric, "" for characters, and FALSE for logical vectors) can be created via the vector function, specifying the mode (vector type) and length:

> vector(mode = "numeric", length = 2)
[1] 0 0
> vector(mode = "character", length = 10)
 [1] "" "" "" "" "" "" "" "" "" ""
> vector(mode = "logical", length = 3)
[1] FALSE FALSE FALSE

Creating repetitive vectors

You have already learned the two ways to create consecutive vectors with : and seq. Another special type of vector, a repetitive vector, can be created with the rep function (which stands for replicate). We simply need to specify what to replicate and how many times to replicate it:

> rep(x = 22, times = 10)
[1] 22 22 22 22 22 22 22 22 22 22

The rep function can operate on vectors longer than 1 as well:

> x = c(18, 0, 9)
> rep(x, 3)
[1] 18  0  9 18  0  9 18  0  9

Substrings

Another useful function with characters is substr, which is used to extract subsets of character strings, that is, we create a subset of the characters within an individual element of a vector (substring), rather than a subset of the vectors elements (see the next section). The function requires the start and stop values. Let's take a look at the following examples:

> x = "Subsetting strings"
> substr(x, start = 1, stop = 14)
[1] "Subsetting str"
> substr(x, 6, 14)
[1] "tting str"
> substr(x, 1, 3)
[1] "Sub"

As we can see, the start and stop values are considered inclusive. For example, the last expression, where start is equal to 1 and stop is equal to 3, gives us the three characters occupying places 1 to 3 within the character string x.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.104.242