R Programming
- Overview of R
- Installing R on Windows
- Download and Install RStudio on Windows
- Setting Your Working Directory (Windows)
- Getting Help with R
- Installing R Packages
- Loading R Packages
- Take Input and Print in R
- R Objects and Attributes
- R Data Structures
- R – Operators
- Vectorization
- Dates and Times
- Data Summary
- Reading and Writing Data to and from R
- Control Structure
- Loop Functions
- Functions
- Data Frames and dplyr Package
- Generating Random Numbers
- Random Number Seed in R
- Random Sampling
- Data Visualization Using R
Getting Help with R
The help() function and ? help operator in R provide access to the documentation pages for R functions, data sets, and other objects, both for packages in the standard R distribution and for contributed packages.
The help() function and ? help operator in R provide access to the documentation pages for R functions, data sets, and other objects, both for packages in the standard R distribution and for contributed packages.
To get general help just type the below command
help.start()
To access documentation for the standard lm (linear model) function, for example, enter the command.
help(lm)
OR
help("lm")
OR
?lm
OR
?"lm"
You can use any of the four commands all serves same purpose only. As you can see the quotes are optional.
To access help for a function in a package that’s not currently loaded, specify in addition the name of the package: For example, to obtain documentation for the rlm() (robust linear model) function in the MASS package, help(rlm, package=”MASS”).
help(rlm, package="MASS")
To see an example of a particular function just type the function name within example() function. Below command will show an example of lm() function.
example(lm)
Vignettes and Code Demonstrations using browseVignettes(), vignette() and demo()
Many packages include vignettes, which are discursive documents meant to illustrate and explain facilities in the package. You can discover vignettes by accessing the help page for a package, or via the browseVignettes() function. Below command shows show available vingettes.
vignette()
Below command shows the vignettes of ggplot2 package.
browseVignettes(package="ggplot2")
You can also use the vignette(“vignette-name”) command to view a vignette (possibly specifying the name of the package in which the vignette resides, if the vignette name is not unique): for example, vignette(“timedep”) or vignette(“timedep”, package=”survival”) (which are, in this case, equivalent).
vignette("timedep")
#or
vignette("timedep", package="survival")
There are internet search sites that are specialized for R searches, including search.r-project.org (which is the site used by RSiteSearch) and Rseek.org. Below command will give searches about caret package.
RSiteSearch("caret")
For example, RSiteSearch(“{generalized linear model}”) returns information about R functions, vignettes, and CRAN task views related to the term “generalized linear model” without matching the individual words “generalized”, “linear”, or “model”.
RSiteSearch("{generalized linear model}")
The apropos() function searches for objects, including functions, directly accessible in the current R session that have names that include a specified character string. This may be a literal string or a regular expression to be used for pattern-matching (see ?”regular expression”). By default, string matching by apropos() is case-insensitive. For example, apropos(“^glm”) returns the names of all accessible objects that start with the (case-insensitive) characters “glm”.
apropos("^glm")
R comes with a number of sample datasets that you can experiment with. Type data( ) to see the available datasets.
data( )
The results will depend on which packages you have loaded. Type help(datasetname) for details on a sample dataset.
help(iris) #This will show the help file for iris dataset.
Get arguments of a function by using the function args(). To get the arguments for “rnorm()” function type the below command.
args("rnorm")
Output:
function (n, mean = 0, sd = 1)
NULL