Biostatistics with R

R sessions

1. Starting an R session

The R programming can be done in two ways. We can either type the command lines on the screen inside an "R-session", or we can save the commands as a "script" file and execute the whole file inside R. First we will learn the R-session.

To start an R session, type 'R' from the command line in windows or linux OS. For example, from shell prompt '$' in linux, type

$ R
This generates the following output before entering the '$>$' prompt of R:
R version 3.1.1 (2014-07-10) -- "Sock it to Me" Copyright (C) 2014 The R Foundation for Statistical Computing Platform: x86_64-unknown-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. [Previously saved workspace restored]

Working with R session

Once we are inside the R session, we can directly execute R language commands by typing them line by line. Pressing the enter key terminates typing of command and brings the > prompt again. In the example session below, we declare 2 variables 'a' and 'b' to have values 5 and 6 respectively, and assign their sum to another variable called 'c':

> a = 5 > b = 6 > c = a + b > c
The value of the variable 'c' is printed as,
[1] 11

In R session, typing a variable name prints its value on the screen.

Get help inside R session

To get help on any function of R, type help(function-name) in R prompt. For example, if we need help on "if" logic, type,

> help("if")
then, help lines for the "if" statement are printed.

Exit the R session

To exit the R session, type quit() in the R prompt, and say 'n' (no) for saving the workspace image. This means, we do not want to save the memory of all the commands we typed in the current session:

> quit()
Save workspace image? [y/n/c]: n

Saving the R session

Note that by not saving the current session, we loose all the memory of current session commands and the variables and objects created when we exit R prompt.

When we work in R, the R objects we created and loaded are stored in a memory portion called workspace. When we say 'no' to saving the workspace, we all these objects are wiped out from the workspace memory. If we say 'yes', they are saved into a file called ".RData" is written to the present working directory.

In Linux, this "working directory" is generally the directory from where R was started through the command 'R'. In windows, it can be either "My Documents" or user's home directory.

When we start R in the same currnt directory next time, the work space and all the created objects are restored automatically from this ".RData" directory.

Listing the objects in the current R session

We can list the names of the objects in the current R session by ls() command. For example, start R session fresh and proceed as follows:
> > a = 5 > b = 6 > c = 8 > sum = a+b+c > sum
[1] 19
> ls()
[1] "a" "b" "c" "sum"
Here, the objects we created have been listed.

Removing objects from the current R session

Specific objects created in the current session can be removed using rm() command. If we specify the name of an object, it will be removed. If we just say rm(list = las()) , all objects created so far will be removed. See below:

> a = 5 > b = 6 > c = 8 > sum = a+b+c > sum
[1] 19
> ls()
[1] "a" "b" "c" "sum"
> rm(list=c("sum")) > ls()
[1] "a" "b" "c"
> rm(list = ls()) > ls()

Getting and setting the current working directories

From R prompt, we can get information about the current working directory using getwd() command:

> getwd()
[1] "/home/user"
Similarly, we can set the current wor directory by calling setwd() function:

> setwd("/home/user/prog")

After this, "/home/user/prog" will be the working directory.

In Windows version of R, the working directory can be set from menu in R window.

Getting file information from R session

When we are inside R prompt, the operation system commands will not be recognised by R. If we want to list the names of files in the current directory in which R has been started, we should use list.files() commnd to list the files. This lists all the files in the current directory.

In case we need information on a specific file, use"filename") command. This prints all the information about this file on the screen.