A Little Book of R For Time Series, Release 0.2
Alternatively, you can use the name of the fourth element in the table (“John”) to find the value of that table
element:
> mytable[["John"]]
[1] 2
Functions in R usually require arguments, which are input variables (ie. objects) that are passed to them, which
they then carry out some operation on. For example, the log10() function is passed a number, and it then calculates
the log to the base 10 of that number:
> log10(100)
2
In R, you can get help about a particular function by using the help() function. For example, if you want help
about the log10() function, you can type:
> help("log10")
When you use the help() function, a box or webpage will pop up with information about the function that you
asked for help with.
If you are not sure of the name of a function, but think you know part of its name, you can search for the function
name using the help.search() and RSiteSearch() functions. The help.search() function searches to see if you
already have a function installed (from one of the R packages that you have installed) that may be related to some
topic you’re interested in. The RSiteSearch() function searches all R functions (including those in packages that
you haven’t yet installed) for functions related to the topic you are interested in.
For example, if you want to know if there is a function to calculate the standard deviation of a set of numbers, you
can search for the names of all installed functions containing the word “deviation” in their description by typing:
> help.search("deviation")
Help files with alias or concept or title matching
'deviation' using fuzzy matching:
genefilter::rowSds
Row variance and standard deviation of
a numeric array
nlme::pooledSD Extract Pooled Standard Deviation
stats::mad Median Absolute Deviation
stats::sd Standard Deviation
vsn::meanSdPlot Plot row standard deviations versus row
Among the functions that were found, is the function sd() in the “stats” package (an R package that comes with
the standard R installation), which is used for calculating the standard deviation.
In the example above, the help.search() function found a relevant function (sd() here). However, if you did not
find what you were looking for with help.search(), you could then use the RSiteSearch() function to see if a search
of all functions described on the R website may find something relevant to the topic that you’re interested in:
> RSiteSearch("deviation")
The results of the RSiteSearch() function will be hits to descriptions of R functions, as well as to R mailing list
discussions of those functions.
We can perform computations with R using objects such as scalars and vectors. For example, to calculate the
average of the values in the vector myvector (ie. the average of 8, 6, 9, 10 and 5), we can use the mean() function:
> mean(myvector)
[1] 7.6
We have been using built-in R functions such as mean(), length(), print(), plot(), etc. We can also create our own
functions in R to do calculations that you want to carry out very often on different input data sets. For example,
we can create a function to calculate the value of 20 plus square of some input number:
1.5. A brief introduction to R 9