Easy R for M.F.Sc students – Part 2 (Getting started with R)


Hi friends,

In this chapter, I would be detailing upon basic functionalities of R and what immediate things you should know before exploring and doing analysis with R. You can find my first chapter if you don’t know how to download and install R in your computer.

Now open R in your computer. You will get a window as shown below with some instructions about R.

The basic information tells you R is a free software with no warranty. How to find information about the license, contributors, citation, demo and help. All functions in R start with a text followed by curly brackets. ‘citation ( )’ is a function in R, so that if you type and press ENTER key, R will show you how to cite the software in your article or journal.

Some functions will require an input to work. Such functions comes with a set of arguments.  For example, lets take a function in R which calculates mean of a vector of numbers.

mydata <- c (1, 2, 3, 4, 5 )

In the example above, I created a vector of numbers with concatenating function which is ‘c ( )’. To see what R understood, simply type ‘mydata’ and press ENTER key.

> mydata
[1] 1 2 3 4 5

Now you already learned how to create a vector in R. To calculate the mean of this vector, use function ‘mean ( )’.

> mean (mydata)
[1] 3

Here we used ‘mydata’  as an input for the function ‘mean ( )’. This is mandatory for the function to work. You will get an error, if you don’t provide an input. See below:

> mean ( )
Error in is.numeric(x) : 'x' is missing

The error says, something is missing and obviously that is the data for which you want to find the mean. The ‘mean ( )’ function accepts more inputs but they are not mandatory because by default it assume the input for those arguments if not mentioned. To see all arguments of a function, you can type a question mark followed by the function name as follows…

? mean ( )

You can get help for all of R functions, using the help (function) or ? function. If you don’t know the name of the function that do a specific task, you can probably use help.search (“type your key words inside this bracket within quotes”). A short reference card for most useful functions can be found here (click)

The help command (?mean or help (mean)) will open a HTML page giving all properties of the function.

In the HTML page, you can see for which arguments, the function accept default values.

## Default S3 method:
mean(x, trim = 0, na.rm = FALSE, ...)

In this function,’x’ is a mandatory argument for which the user must give an input. The other optional arguments are ‘trim=’ and ‘na.rm=’ for which if the user wish can provide different values. If not, R will accept the default values and compute the mean of the data. To make it clear, let’s take the optional argument ‘na.rm=’. The definition for each argument is given in the HTML help page.

The argument ‘na.rm=’ will be useful when you have missing data in your vector. The default input value for this argument is ‘FALSE’ which means, there is no missing value in your data. So let’s try to include a missing value and see what the funtion gives us:

> mydata<-c(1,2,NA,3,4,5)
> mydata
[1]  1  2 NA  3  4  5
> mean(mydata)
[1] NA

Here, I introduced an extra element named ‘NA’ which means a missing element in R. The mean function gave us ‘NA’ which is not the output we need. This happened because R took the default value for the argument ‘na.rm= FALSE’. If we change it to ‘TRUE’, this function will work.

> mean (mydata, na.rm = TRUE)
[1] 3

Suppose you don’t want to repeat this analysis in R. In that case you can save this work in your computer and continue next time when you open R.  The window on which you work in R is called ‘R Console’. Everything you have done so far in R console are loaded in a ‘workspace’. You should save this R workspace in your computer, if you want to continue with your analysis next time. When you close R, by default it will ask you whether you want to save the workspace. Let’s try to do this.

But before that, let’s see what we have in our R workspace. Every information in R exists as ‘Objects’. To see the objects in your R workspace, type:

> ls ( )
[1] "mydata"

The ‘ls ( )’ function list all the objects we have in our current workspace. So if you save this workspace and open next time, you will be already having ‘mydata’. So then you don’t need to create that data again. Let’s add more objects to our workspace.

> A<-35
> B<- 0
> C<- c (35, 50)
> ls ( )
[1] "A"      "B"      "C"      "mydata"

In the above script, I added three more objects by creating  ‘A’, ‘B’ and ‘C’. Let’s save this workspace now in desktop of our computer. Workspace will be saved as files with an extension “.RData”. So if you save the workspace with a name ‘data’, they will carry the extension ‘data.RData’. You probably won’t see this extension in your desktop depending upon the configuration of your computer. By default, the files with extension RData will display the R icon. Go to ‘File’ in the top menu and choose ‘Save workspace’. Save the file in your desktop with a name of your choice.

Now close R. It will ask whether you want to save workspace. Choose ‘No’, since we already saved it in our desktop. Now double click the saved workspace in your desktop, the ‘.RData’ file. This will open R and you will see a window like below.

Now this R console is different from the one we saw in the beginning. In this console, we have an extra comment or text saying “[Previously saved workspace restored]“. This means, the R console is not a fresh window and you have some objects already loaded into the workspace. To see the objects, type ‘ls ( )’ and you will see all the previously created objects : ‘A’, ‘B’, ‘C’and ‘mydata’. Type ‘mydata’and press ENTER key to view the data created in the beginning of our tutorial.

To remove objects in a working space, you can use the following function:

rm (x)                 #remove x from the workspace
rm ( list = ls ( ) )   #remove all the variables from the workspace

If we don’t save our workspace in a specific location, and we opted to save it while closing R, by default the ‘.RData’ file will be saved in the working directory. When you open R, the workspace will be working on a base directory and every RData files will be saved in this location unless specified. To find this location, type ‘getwd ( )’ in R and press ENTER key.

> getwd ( )
[1] "C:/Users/Pazhayamadom/Rtutorial"

The location you would get will be a different one. You can change this location by the function ‘setwd ( )’ with the preferred location inside brackets. Remember that R don’t understand single backslash. R either understand a single forward slash or a double backward slash. To set the working directory to desktop, following is an example in my computer.

> setwd ("C:/Users/Pazhayamadom/Desktop")
> setwd ("C:\\Users\\Pazhayamadom\\Desktop")

If you have any .RData files in your default working directory, R will load this workspace everytime when you start R. This will be a nuissance and might get you wrong results since you already have some numbers assigned for A, B , C and mydata in the case of above example. So always choose not to save the workspace when you close R. But if you want to save the workspace, save it using the file menu and choose a different location other than the default working directory.

If your R is already having a previously saved workspace restored text when you open it, you can solve this issue by deleting the .Rdata file from default working directory or moving that file to some other location if you don’t want to delete it.

I hope you enjoyed this chapter and please leave feedback. It will inspire and motivate me to write in a better way in future. Thank you all.

About Deepak George Pazhayamadom

I'm a fish biologist and a mathematical modeller. I have a wide range of research interests, mostly centered on fisheries resource management.

Posted on December 31, 2011, in Bio-Statistics and Analysis. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: