Introduction to R

The aim of the present document is to give a starting point for beginners interested in R. R is a rich environment for data handling, data analysis and graphical display. The possibilities offered by R are vast for two main reasons: on one hand, R includes a simple and effective programming language; on the other hand, it has been extended by a large number of packages (i.e., collections of R functions and datasets). The standard (or base) packages are automatically available in any R installation, whereas the contributed packages are mostly available for download from CRAN repository (https://CRAN.R-project.org/). Given its popularity, more and more web sites offer assistance with R. Here we report a short list of some of the main internet resources:

http://www.r-project.org 

http://lib.stat.cmu.edu/R/CRAN 

http://www.statmethods.net

RStudio (http://www.rstudio.org/) provides an easy graphical user interface (GUI) for R. In what follows, we assume that both R and RStudio have been successfully installed on your system and that RStudio has been open. By issuing the following command in the RStudio console, you can get access to useful manuals, references and additional materials:

 

Return to the wiki
home page
> help.start()
↑ Go up

To install R packages, use the ‘install.packages’ command  with the name of the package inside the parentheses. Remember that the name of the package must be surrounded by quotation marks. As an example, we are going to install the ggplo2 package.

 
> install.packages('ggplot2')
 

After installation, you need to load the package if you want to access its content. The ‘library’ command is aimed at this purpose. You can specify the package name with or without quotation marks.

 
> library(ggplot2)
 

To see which packages are currently loaded, use

 
> search()
↑ Go up

However, some packages may be loaded without appearing in the search list. To visualize these packages, use

 
> loadedNamespaces()
 

In general, to check which packages are installed at your system, issue the command

 
> library()
 

with no arguments. It is worth noting the use of parentheses in R. If you type the name of a function (e.g., library) without parentheses, R will display the content of that function without executing it. 

To obtain information about a given package (e.g., stats), issue the following command

 
> help('stats')
↑ Go up

or

 
> ?stats