Chapter 2. The R Environment

R is currently one of the most popular programming environments for statistical computing. It was evolved as an open source language from the S programming language developed at Bell Labs. The main creators of R are two academicians, Robert Gentleman and Ross Ihaka, from the University of Auckland in New Zealand.

The main reasons for the popularity of R, apart from free software under GNU General Public License, are the following:

  • R is very easy to use. It is an interpreted language and at the same time can be used for procedural programming.
  • R supports both functional and object-oriented paradigms. It has very strong graphical and data visualization capabilities.
  • Through its LaTex-like documentation support, R can be used for making high-quality documentation.
  • Being an open source software, R has a large number of contributed packages that makes almost all statistical modeling possible in this environment.

This chapter is intended to give a basic introduction to R so that any reader who is not familiar with the language can follow the rest of the book by reading through this chapter. It is not possible to give a detailed description of the R language in one chapter and the interested reader should consult books specially written in R programming. I would recommend The Art of R Programming (reference 1 in the References section of this chapter) and R Cookbook (reference 2 in the References section of this chapter) for those users who are mainly interested in using R for analyzing and modeling data. For those who are interested in learning about the advanced features of R, for example, for writing complex programs or R packages, Advanced R (reference 3 in the References section of this chapter) is an excellent book.

Setting up the R environment and packages

R is a free software under GNU open source license. R comes with a basic package and also has a large number of user-contributed packages for advanced analysis and modeling. It also has a nice graphics user interface-based editor called RStudio. In this section, we will learn how to download R, set up the R environment in your computer, and write a simple R program.

Installing R and RStudio

The Comprehensive R Archive Network (CRAN) hosts all releases of R and the contributed packages. R for Windows can be installed by downloading the binary of the base package from http://cran.r-project.org; a standard installation should be sufficient. For Linux and Mac OS X, the webpage gives instructions on how to download and install the software. At the time of writing this book, the latest release was version 3.1.2. Various packages need to be installed separately from the package page. One can install any package from the R command prompt using the following command:

install.packages("package name")

After installing the package, one needs to load the package before using it with the following command:

library("package name")

A very useful integrated development environment (IDE) for R is RStudio. It can be downloaded freely from http://www.rstudio.com/. RStudio works on Windows, Linux, and Mac platforms. It has both a desktop version and also a server version that can be used for writing R programs through a browser interface on a remote server. After installing R and RStudio, it is useful to set the default working directory to the directory of your choice. RStudio reads and writes files containing R codes into the working directory. To find out what the current directory is, use the R command getwd( ). To change the working directory to a directory of your preference, use the following command:

setwd("directory path")

You can also set this from the menu bar of RStudio by clicking on Session | Set Working Directory.

Your first R program

Let us write a simple program to add two integers x and y resulting in their sum z. On the command prompt in RStudio, type the following commands and press Enter:

>x <-2
>y <-3
>z <-x+y
>print(z)
[1]  5

Now, you can assign different values to x and y and print z to see how z changes. Instead of print(z), you can also simply enter z to print its values.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.76.164