2014年10月2日星期四

R Training: Chapter 1. Introduction to R


Chapter 1. Introduction to R
Saturday, September 27, 2014
R is, at its heart, an elegant and beautiful language, well tailored for data analysis and statistics. --- Hadley Wickham
For introduction of R language, you are recommended to read the first chapter of R in Action and the introduction part of Advanced R.

R Installation

Install R
You can follow my instruction which is described below to install and upgrade R on Windows.
First, you need to download R and RStudio and install them. After the installations, run the following codes to set up a global library.
chooseCRANmirror() # Choose XMU
source("http://www.r-statistics.com/wp-content/uploads/2010/04/upgrading-R-on-windows.r.txt")
Old.R.RunMe()

Upgrade R
Once you have done these, from now on, whenever you want to update to a new version of R in the future, all you will need to do are the following TWO steps:
  1. Download and install the new version of R
  2. Open your new R and run the following codes
source("http://www.r-statistics.com/wp-content/uploads/2010/04/upgrading-R-on-windows.r.txt")
New.R.RunMe()

RStudio settings
Open up your RStudio. In RStudio, Tools --> Global Options --> Code Editing/Appearance. See Customizing RStudio for details.
Most used RStudio keyboard shortcuts:
Description
Keyboard(Windows)
Clear console
Ctrl+L
Interrupt currently executing command
Esc
Run current line/selection
Ctrl+Enter
Run current document
Ctrl+Alt+R
Find and Replace
Ctrl+F
Find in Files
Ctrl+Shift+F
Comment/uncomment current line/selection
Ctrl+Shift+C
Check Spelling
F7
Undo
Ctrl+Z
Redo
Ctrl+Shift+Z
Delete Line
Ctrl+D
Indent
Tab (at beginning of line)
Show help for function at cursor
F1
Show source code for function at cursor
F2
Attempt completion
Tab or Ctrl+Space
See Keyboard Shortcuts for more details.

Getting help
Use Ctrl+Enter to run the selected codes or the line where you cursor on. The output is shown in the Console window.
help.start()   # general help
help(plot)      # help about function plot
?plot          # same thing 
apropos("plot") # list all functions containing string plot
example(plot)   # show an example of function plot

# search for plot in help manuals and archived mailing lists
RSiteSearch("plot")

# get vignettes on using installed packages
vignette()      # show available vingettes
vignette("knitr-html") # show specific vignette

Manage your workspace
Now please create a file in you computer system as your workpalce. Such as E:\Project\WISE R Club\LearnR.
R gets confused if you use a path in your code like
c:\mydocuments\myfile.txt
This is because R sees "\" as an escape character.
Instead, you should use
c:\\my documents\\myfile.txt
c:/mydocuments/myfile.txt
Either will work.
getwd() # print the current working directory - cwd 
ls()    # list the objects in the current workspace

setwd("E:/Project/WISE R Club/LearnR"# note / instead of \ in windows 

# view and set options for the session
help(options) # learn about available options
options() # view current option settings
optio#ns(digits=3) # number of digits to print on output

# work with your previous commands
history() # display last 25 commands
history(max.show=Inf) # display all previous commands

# save your command history 
savehistory(file="myfile") # default is ".Rhistory" 

# recall your command history 
loadhistory(file="myfile") # default is ".Rhistory"

# save the workspace to the file .RData in the cwd 
save.image()

# save specific objects to a file
# if you don't specify the path, the cwd is assumed 
save(object list, file="myfile.RData")
save(x, file="mydata.RData")

# load a workspace into the current session
# if you don't specify the path, the cwd is assumed 
load("mydata.RData")

q() # quit R. You will be prompted to save the workspace.

Script input/output
By default, R provides an interactive session with input from the keyboard and output to the screen. However, you can have input come from a script file and direct output to a variety of destinations.

Input
# source a script
source("myfile.R")
source("myfile.R", print.eval = TRUE)
source("myfile.R", echo = TRUE, print.eval = TRUE)

Output
The sink( ) function defines how to print the output.
# direct output to a file 
sink("output_file", append=FALSE, split=FALSE)

# return output to the terminal 
sink()
The append option controls whether output overwrites or adds to a file. The split option determines if output is also sent to the screen as well as the output file.
Here are some examples of the sink() function.
# output directed to myfile.txt in cwd. output is appended to existing file. output also send to terminal. 
sink("output_file.txt", append=FALSE)
x <- 1:5
cat("x: \n")
x
cat("Mean: \n")
mean(x)
cat("Variance: \n")
var(x)

cat("\n")
source("myfile.R", echo = TRUE, print.eval = TRUE)

sink()
When redirecting output, use the cat( ) function to annotate the output.

Packages
Packages are collections of R functions, data, and compiled code in a well-defined format. The directory where packages are stored is called the library.
.libPaths() # get library location 
library()   # see all packages installed 
search()    # see packages currently loaded
A complete list of contributed packages is available from CRAN.
You can add packages from the Tools --> Install Packages or run code like install.packages("ggplot2"). You can update packages from Tools --> Check for Packages Updates or use update.packages()

Reusing results
One of the most useful design features of R is that the output of analyses can easily be saved and used as input to additional analyses. Please see the following examples.
lm(mpg~wt, data=mtcars)

fit <- lm(mpg~wt, data=mtcars)

str(fit) # view the contents/structure of "fit"

# plot residuals by fitted values
plot(fit$residuals, fit$fitted.values)

# produce diagnostic plots
plot(fit) 

An example
An example at the end.
setwd("E:/Project/WISE R Club/LearnR")
install.packages("ggplot2")
library(ggplot2)
help(package = "ggplot2")
vignette(package = "ggplot2")
?qplot
str(diamonds)
example(qplot) # example of qplot function
qplot(color, price/carat, data = diamonds, geom="jitter", alpha = I(1/5))
Notices: R is a case sensitive language.

References and resources

References

Resources
The following online resources are also very helpful for R language learning. I suggest you explore some of them by yourself.