Sunday, 31 March 2013

R: Importing Data

There are number of ways in importing data into R, and several formats are available,
• From Excel to R;
• from SPSS to R; and,
• from Stata to R, and more here.
In this post, we are going to talk about importing common data format that we often encounter, such as Excel and Text data. Most of the data are saved in MS Excel, and the best way to import this is to save this in CSV format, below is the procedure:
• Open your Excel data;
• go to File > Save As or press Ctrl+Shift+S;
• name this with anything you want, say "Data". Then before clicking Save, make sure to change the File Format to Comma Delimited Text and better set the directory to My Documents folder, for Windows.
• when saved, this file will have a name "Data.csv".
Now open R, and run the following

The argument header = TRUE tells R that the first row of the data are the labels of every column. If set to FALSE, means the first row of the data are not the labels, but are considered as data points.

Saturday, 30 March 2013

R: How to Enter your Data?

Every experiment starts with data, so the question is "how do you enter your data into R?". Well there are many ways to do that, we can do import for large dataset. But for this post, we will only consider the two functions below:
• The concatenate, c; and,
• the data.frame functions.
The concatenate function, c, is use for combining data points into single numeric R object, known as the vector. The usage of this function is simply

Where ... is the objects to be concatenated. Run ?c, for description of the second argument. Let's try an example,

What happened here is that, we defined a new object, vec1, into the workspace. That means, we can start manipulating the entries of it, say the summary,

For dispersion, try this,

What about the data.frame function? If the first function combines data points into a single vector, data.frame from the name itself constructs a frame of data points. Here is an example,

What we did here is we defined two R objects, the weights and volunteers, then we combine the two into a table like structure, called the data frame. To extract columns of data1, try this,

And the mean of the weights is,

There are two ways to install R in Ubuntu. One is through the terminal, and the other is through the Ubuntu Software Center.

Through the Terminal
• Press Ctrl+Alt+T to open the Terminal;
• then execute sudo apt-get update; after that,
• run sudo apt-get install r-base;
To run R, execute R in the Terminal (see the picture below).

 R statistical package in Ubuntu 12.10 Terminal

Through Ubuntu Software Center
• Open Ubuntu Software Center;
• search for r-base;
• and click Install;
• then run R by executing R in the Terminal.
Working in the Terminal would be inconvenient, so I suggest downloading a user-friendly interface. For example in Ubuntu, I recommend using RStudio IDE or RKWard KDE.