Easy R for M.F.Sc students – Part 2 (Getting started with R)
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 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)  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:
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:
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)  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 ( )  "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.
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:
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 ( )  "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.
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.