Title: An introduction to R
1An introduction to R
- Xueli Liu
- STA4502/5507
- October 1, 2007
2R and statistics
Packaging a crucial infrastructure to
efficiently produce, load and keep consistent
software libraries from (many) different sources
/ authors Statistics most packages deal with
statistics and data analysis State of the art
many statistical researchers provide their
methods as R packages
3R as a calculator
- log2(32)
- sqrt(2)
- seq(0, 5, length6)
- plot(sin(seq(0, 2pi, length100)))
- q()-quit R session
4Variables
gt a 49 gt sqrt(a) gt a "The dog ate my
homework" gt sub("dog","cat",a) 1 "The cat ate
my homework gt a (113) gt a 1 FALSE
numeric
character string
logical
5vectors, matrices and arrays
- vector an ordered collection of data of the same
type - a c(1,2,3)
- a3
- gt a2
- matrix a rectangular table of data of the same
type - b matrix(999, 10,4)
- b
6Branching
if (logical expression) statements else
alternative statements else branch is optional
7Loops
for(i in 110) print(ii) i1 while(ilt10)
print(ii) iisqrt(i)
8Importing and exporting data
- To deal with rectangular tables in the form of
tab-delimited text files. - gt x read.delim(filename.txt)
- also read.table, read.csv
- write.table(x, filex.txt, sep\t)
- setwd change directory
9Getting help
Details about a specific command gt?
t.test help.search(keyword)
10Preparing Computer Output for Printing
- Save command as a script file for later use
- copy-and-paste that portion of the R commands and
output of interest into an editor of some kind
Include only those parts that you need
cut-and-paste as you go along. - Command we need so far
- ?shapiro.test
- ?wilcox.test
- ?ks.test
- ?rank
11Web sites
Reference card for basic functions
http//cran.r-project.org/doc/contrib/refcard.pdf
Downloading and help search www.r-project.org cr
an.r-project.org http//www.stat.ufl.edu/xueli/st
a6329/sta6329.html