RBigData NTL
RBigData NTL
RBigData NTL
Introduction to R
1. Basics
2. How R works
3. What is special about R
4. How to be clever in R
5. R for all your computational needs; programming
6. Graphics in R — to explore, to inform, to impress
7. Expanding your computational skills and experience
First steps. R as a calculator
A session:
Your work in R between Start and Finish
2
The basic rules of R
Assignment A <- 5
Check your workspace ls()
Display the value of A A
Use A in an expression (A + 4)^2 / 107.3
Create a new object using A B <- log((A + 4)^4 + 17)
Remove A from workspace rm(A)
System-defined objects Functions sqrt, exp, round, . . . ,
Constants pi, datasets
User-defined objects A
Apply a function sqrt(A + 5.2)
4
R as a calculator. Scalar operations
A valid name (syntax) has to be used for an object used for assignment
alpha-numeric
The value of an object can be over-written
assignment to an object that has already been defined
5
From scalars to vectors
Operations on vectors:
length(A4B); names(A4B)
round — see above
Examples:
help(round); help(seq); args(floor)
7
Vectors generated by the system
Random numbers
runif(500, 2, 8)
rnorm, rgamma, rbeta, rchisq, rt, . . .
— use help to learn about them
9
Type of objects and their attributes
vectors
functions is.vector, as.vector
scalar is a vector (of length 1)
matrices and arrays — matrix, is.matrix, as.matrix
lists — list, is.list, as.list
collections of objects
functions — function, is.function
data frames — data.frame, is.data.frame
user-defined types
10
Numeric, character and logical
as.numeric, is.numeric
as.character, is.character
as.logical, is.logical
AB * (CC > 0)
CC > 0 is a logical vector,
but in the numeric operation it is interpreted as 0/1
11
Character and logical functions
Logical values: T, F
Logical operators:
==, &, |, !=, !
12
Naming and subsetting
vector AB;
names(AB) <- c("First", "Second", "Third", "Tabasco",
"Quintana Roo", ...)
AB[seq(6)] — the first 6 elements of AB
Subsetting:
by element No.s: ls()[seq(50)]
by names: AB[c("First", "Second")]
by a logical vector (T — include; F — exclude)
by negatives of elem. No.s — elem.s to exclude (e.g. vec[-seq(4)])
13
A bit of fun
Histogram:
hist(rnorm(20000, 1.7, 4.2))
Study help(hist)
Function plot
plot(vec)
Plot types:
argument type=, values: "n", "p", "l", "b"
Loops:
for (i in vec)
{
R code (involving i)
}
Conditional loops:
while (condition)
{
R code
}
Example: Iterative algorithms (e.g., GLM)
18
Matrices
— two-dimensional arrays
MAT <- matrix(data=seq(16), nrow=8, ncol=6,
dimnames=seq(8), LETTERS(6))
diag(vec)
Submatrices:
MAT[, seq(3)]; MAT[c(3, 7, 3, 8), c("C", "E", "A")]
MAT[sort.list(MAT[, 2]), ]
Repetition, using conditions, exclusion, etc.
19
Working with matrices
Output:
write.csv, write.sas, write.dta
Example:
LST <- list() ## An empty list
for (i in seq(10))
LST[[i]] <- seq(i)
Operating on a list:
lapply(LST, sum) ## List of the within-element totals
22
Lists II
Example:
ExtrC <- function(mat, cls)
mat[, cls]
lapply(LST, ExtrC, 4) ## The 4th cols pf elem.s of LST
23
Summary (syntax)
+, -, /, *, %*%, ^, sum, prod, |, &, !, !=, <, >, <=, >=, " "
24