Home

R Functions for Statistical Analysis

|
Updated:  
2017-07-04 0:34:45
|
Statistical Analysis with R Essentials For Dummies
Explore Book
Buy On Amazon
Functions built into R. Each one consists of a function name immediately followed by parentheses, such as c(), sum(), mean(), and var(). Inside the parentheses are the arguments. In this context, "argument" doesn't mean "disagreement," "confrontation," or anything like that. It's just the math term for whatever a function operates on.

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

These four R functions 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.

For example, 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 number 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" Even this works:

> substr(stop=4, start=2,"abcdefg") [1] "bcd"

So when you use a function, you can place its arguments out of order, if you name them. R calls this keyword matching, which comes in handy when you use an R function that has many arguments. If you can't remember their order, just use their names and the function works.

If you ever need help for a particular function — substr(), for example — type ?substr and watch helpful information appear on the Help tab.

About This Article

This article is from the book: 

About the book author:

Joseph Schmuller, PhD, is a cognitive scientist and statistical analyst. He creates online learning tools and writes books on the technology of data science. His books include R All-in-One For Dummies and R Projects For Dummies.