Getting Started with R
R is a programming language for statistical computing, data manipulation, and graphical display. R is a highly extensible language and has become a popular language for Data Analytics. The R software can be downloaded from https://cran.r-project.org
Topics covered in this section are
➤ Getting Help
➤ Installed Packages and Objects
➤ In-built Data Sets
➤ Executing in-built examples
➤ Saving, loading, and removing R data structures
Getting Started with R
↪ Getting Help
The help() function or '?' operator can be used for finding help for objects, functions, and data sets.
help() # Shows help documentation help("read.table") # help for read.table() function ?read.csv # help for read.csv() function help(package="MASS") # help for package MASS. help(lm.gls, package="MASS") # help for function lm.gls() under package MASS help(package=curl) # help for package mass. Note: double quote is optional
The help.search() function searches the documentation for packages installed. The search can be based on pattern matching.
help.search("list") help.search("^lm") # search can be based on pattern matching
The ?? operator is a synonym for help.search() function.
??list ??"^Date" ??read.table
Getting Started with R
↪ Getting Help
The help.start() starts the HTML version of R’s documentation.
help.start() help.start("list")
Packages may include vignettes, which are meant to illustrate and explain facilities in the R package. The browseVignettes() function displays vignettes from installed packages in the browser.
browseVignettes() browseVignettes(package="glue") # Vignettes in package glue displayed in the browser
The vignette() function displays a list of vignettes in text form.
vignette() vignette(package="glue") # Vignettes in package glue displayed in the browser
Getting Started with R
↪ Installed Packages and Objects
The installed.packages() function lists installed packages in R.
installed.packages()
The above function lists all the details of installed packages. The first column display package names.
installed.packages()[,1] # Display first column of the installed packages
The objects() and ls() functions can be used for listing the names of the objects, data sets, and functions available or defined in the workspace. The data is not displayed when there is no data in the workspace.
objects() # Lists all objects available in the current workspace ls() # Same as objects() objects(pattern = "d") # Display the objects that has 'd' in it
The apropos() function searches objects and functions, directly accessible in the current R session. The apropos(“^lm”) returns the names of all accessible objects that start with the character “lm”.
apropos("^lm")
The library() function display all available packages in the libraries specified by lib.loc – a character vector describing the location of R library trees to search through.
help(library) library()
Getting Started with R
↪ In-built Data Sets
There are example data sets available within R along with loaded packages. The library(MASS) loads the package MASS (for Modern Applied Statistics with S) into memory. The data() function will list all the data sets in the loaded packages. The data(Animals) will load the data set Animals into memory. Then type Animals to display the data available in the Animals data set.
library(MASS) data() data(Animals) Animals help(Animals, package="MASS") # Get the help on Animals data set
The typeof() function determines the R internal type or storage mode of any object, and the class() function checks the data type.
typeof(Animals) # Display [1] "list" class(Animals) # Display [1] "data.frame"
The data() function loads the data set into the memory.
data("Animals") # loads the data set Animals into memory Animals Animals$body # Prints contents of column "body" class(Animals$body) # Display [1] "numeric"
The attach() function attaches a set of R objects to the search path. The above Animals$body call can be simplified using attach().
attach(Animals) # Attaches Animals data set to search path body # Same as Animals$body call
The function detach() removes the data set from the search path.
detach(Animals) body # Display error
Getting Started with R
↪ Executing in-built examples
Help pages for functions generally include executable examples demonstrating how the functions work. The example() function can be used for executing the examples in help pages.
example("list") # Executes examples from list help page example(read.table) # Executes examples from read.table help page
Packages may also include demos. The demo() function lists all demos for all installed packages
demo() # Lists demos from installed packages demo(recursion) # Executes demos from recursion package
Getting Started with R
↪ Saving, loading, and removing R data structures
The ls() and rm() functions show and remove objects from memory.
ls() # Show all the objects in memory dframe # Display dframe object if present rm(m, z) # Removes m and z objects if present ls()
The rm() function with list=ls() argument removes all objects or clear the entire R session.
rm(list=ls())
The save() function saves objects into a file.
save(m, z, x, file = "mydata.Rdata")
The load() function loads the file and recreates the data structure.
load("mydata.Rdata")
The save.image() function saves the R session into the .Rdata file.
save.image()
The q() function can be used for quitting the R session.
q()
Getting Started with R
↪ Summary