Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

Even if a function takes no arguments, you still include the parentheses.

The four R functions I’ve shown you are pretty simple in terms of their arguments
and their output. As you work with R, however, you encounter functions that take
more than one argument.
R provides a couple of ways for you to deal with multiargument functions. One
way is to list the arguments in the order in which they appear in the function’s
definition. R calls this positional matching.
Here’s what I mean. The function substr() takes three arguments. The first is a
string of characters like “abcdefg” , which R refers to as a character vector. The
second argument is a start position within the string (1 is the first position, 2
is the
second position, and so on). The third is a stop position within the string (a num-
ber greater than or equal to the start position). In fact, if you type substr into
the
Scripts pane, you see a helpful pop-up message that looks like this:
substr(x, start, stop)
Extract or replace substrings in a character vector
where x stands for the character vector.
This function returns the substring, which consists of the characters between the
start and stop positions.
Here’s an example:
> substr("abcdefg",2,4)
[1] "bcd"
What happens if you interchange the 2 and the 4?
> substr(“abcdefg”,4,2)
[1] “”
This result is completely understandable: No substring can start at the fourth
position and stop at the second position.
But if you name the arguments, it doesn’t matter how you order them:
> substr(“abcdefg”,stop=4,start=2)
[1] “bcd”

You might also like