Installation and Usage of R, The Statistical Computing Language and Environment

One of the interesting thing that piqued my interest for the past two weeks was picking up R, "a language and environment for statistical computing and graphics.". For me, is just another great graphing tool, in addition to Gnuplot (more on this in future post), to plot graph from the console. To be frank, I really like R, easy to pick up (feels like PHP), and plenty of available resources in term of books and search results.

Why R? Few months back, I've bought a Pedometer and starts to capture my daily steps count in CSV format. Instead of putting these data in Google Sheet, I opted to put this in GitHub instead. First, it allows me to build a habit of making daily commits to GitHub. Second, I can explore different kind of graphical plotting tools. Lastly, which is the most important one, it let me have a general overview and awareness of my sedentary lifestyle over a period. This help me to make necessary adjustment which will affect my health. You can't make any changes if you're not constantly aware of the issue, face it directly, and make the necessary changes.

Installation in Ubuntu, in my case, 15.04 is pretty much straight forward.
$ sudo apt-get install r-base

Next, let's start R (yes, capital R) and plot a simple graph.
$ R

R version 3.1.2 (2014-10-31) -- "Pumpkin Helmet"
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: x86_64-pc-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.

> x <- c(1,2,3)
> y <- c(10,20,30)
> plot(x,y)

The above code will produce the following graph.

Instead of generating the graph through the R interpreter, we can also generate it in batch mode using the 'Rscript' command. Create a new file called 'plot.R' with the follow code. After you've compile it, a PDf file named Rplots.pdf will be generated with the graph.
$ cat plot.R
x <- c(1,2,3)
y <- c(10,20,30)

If we need to install any packages, use this command below. Note that this is through the console.
$ sudo Rscript -e "install.packages('ggplot2', repos='')"

Similarly, If you want to install it through the R interpreter.
> install.packages('ggplot2', repos='')

Or without specifying the repository. You will be prompted for one.
> install.packages('ggplot2')

Having done these steps, it should be sufficient enough to explore more powerful features of R. Have fun!

No comments:

Post a Comment