Saturday

Getting Started with R, RStudio, R Commander

Download R 3.0.2 64-bit for Windows from http://cran.stat.nus.edu.sg/ or http://cran.stat.nus.edu.sg/bin/windows/base/R-3.0.2-win.exe. R is an interpreter program for statistical computing and graphics. Install it.

Download RStudio Desktop IDE v0.98.501 from http://www.rstudio.com/ide/download/ or http://download1.rstudio.org/RStudio-0.98.501.exe. Install it.

Open RStudio program.
In the RStudio program, do the following:
* Toolbar > Create a new R script file
* Save the R script file and name it as rscripttest1.R.
* Type the following text in the rscriptest1.R file:
//
# this is a comment line
# get e^1 value
exp(1) # Ctrl+Enter to execute this line only, 2.718282    
13 %/% 4 # integer divide operation to get 3

# write log base e or ln (long) function using log command
y = log(exp(1))
y # output 1

# plot y = log(x) function 

# highlight four lines below and Ctrl+Enter
x = seq(-1,10,by=0.01)
y = log(x) #log base e 
plot(x,y,type='l') # l stands for line
abline(v=0,h=0) # draw two lines vertical=x=0 and horizontal=y=0
# view the plotted graph in the "Plots" window

//
* Go to the Console window, type "> help(abline)" and press "Enter" key to view help manual about abline command.
* In the RStudio Console window, type the following:
//
# c for concatenate is a built-in function
# c is used to combine elements, don't assign anything to variable named c using assignment symbol
> numbers = c(10,8,3,2,1,4,5,9,6,7)
> sort(numbers)
[1]  1  2  3  4  5  6  7  8  9 10
//

To install R Commander package, type the following in the RStudio Console window:
> install.packages("Rcmdr", dep=TRUE)
The R Commander program is a basic statistics GUI for R.
In the RStudio Console window, type the following to play with the R Commander program:
//
# to load R Commander library and open R Commander program
> library(Rcmdr)
# if you want to open R Commander version 2.0-3 program again, type "Commander()"

R Commander program will be opened.
Create a file named "DataCar.txt" in the C:\Users\username1\Documents folder. Save the following text to the "DataCar.txt" file:
--
"Make" "Model" "Cylinder" "Weight" "Mileage" "Type"
"1" "Honda" "Civic" "V4" 2170 33 "Sporty"
"2" "Chevrolet" "Beretta" "V4" 2655 26 "Compact"
"3" "Ford" "Escort" "V4" 2345 33 "Small"
"4" "Eagle" "Summit" "V4" 2560 33 "Small"
"5" "Volkswagen" "Jetta" "V4" 2330 26 "Small"
"6" "Buick" "Le Sabre" "V6" 3325 23 "Large"
"7" "Mitsbusihi" "Galant" "V4" 2745 25 "Compact"
"8" "Dodge" "Grand Caravan" "V6" 3735 18 "Van"
"9" "Chrysler" "New Yorker" "V6" 3450 22 "Medium"
"10" "Acura" "Legend" "V6" 3265 20 "Medium"
--
R Commander Menu > Data > Import data > from text file, clipboard or URL > Enter name for data set: Car > press OK button
Browse to the "DataCar.txt" file and hit Open button
R Commander toolbar > View data set. Close the Car data set window
R Commander Menu > Statistics > Summaries > Active data set
View the data set Car statistics such as frequency, min, median, mean, max in the RStudio Console window
//
Close the R Commander program by clicking the 'x' icon on the window title bar

RStudio Keyboard Shortcuts
Tab key = Code Completion
F1 = Go to Help or type help(<function name>) in the console window
F2 = Go to Function Definition, view the function source code
Ctrl+Shift+U = Extract Function, highlight first and then give the new function a name
Ctrl+I = reIndent Lines, example will be the two same symbols like two braces, two parentheses
Ctrl+Shift+/ = ReFlow Comment, this command will automatically re-wrap multiline roxygen comments that begin with #'
Ctrl+Shift+C = Comment/UnComment Lines
Ctrl+L = cLear Console in the console window
Ctrl+S = to save the R Script file

No comments:

Measure execution time with Julia, example using sorting algorithms

# random integers between 1 and 100 inclusive, generate thousands of them x = rand ( 1 : 100 , 100000 ) @time sort (x; alg=InsertionSort, r...