Statistical Analysis: an Introduction using R/R/Help on a function
Help on a function
Last modified on 23 July 2009, at 20:05
A common problem in R is knowing which function to use. For instance, we want a function that will produce randomly sampled numbers from 1 to 6. Here's one way of finding out
help.search("random") #list the R functions to do with the word "random"
#The "sample" function is described as “Random Samples and Permutations”: just the thing
help("sample") #get detailed information on the "sample" function
?sample #this is a quicker way of doing the same thing
From the help page, you will see that "sample" picks items at random from a list, the "Usage" section shows that the "sample" function takes up to 4 comma-separated arguments. The "Arguments" section describes what they are:
- x (a vector specifying the list of items from which to choose)
- size (the number of times to pick an item)
- replace (a TRUE or FALSE value to indicate whether items should be put back into the list after each pick)
- prob (a vector of probabilities, used if we are not equally likely to pick each item)
In the "Usage" section, you will see that the last two arguments are followed by an = sign. This gives the default values for those arguments. For example, by default, items are not put back after each pick (sampling is "without replacement"). Since these two arguments have defaults, we do not need to explicitly specify them when using the function.