R basics

How is R different from Java and C?
  • Interactive -- R is interpreted
  • Vector oriented -- most functions support vector arguments and vector indexing
  • Doesn't care too much about types (uses coercion)
  • Vector indexes start at 1
  • Assignment:  <- or ->
  • Command termination: hit enter
  • Case sensitive
  • Coercion:   use as.x to convert to type x.  For example:   as.vector(alpha) converts the matrix alpha into a single column vector.
Useful functions: log, sqrt, exp, floor, ceiling, sum, max, diff, min, cumsum

Data structures (R objects):
  • Everything in R is an object
  • Objects can have attributes: names, dimnames, dim, class, length -- use attributes function to get at information and the attr function to pick out a particular attribute.
  • Atomic classes - character, numeric, integer (L), complex, logical (TRUE/FALSE or T/F)
  • Vectors - ordered collection of objects of same type
  • List - ordered collection of objects --- might be different kinds
  • Data frames -- spreadsheet with columns have different kinds of items or list with every element having same length (have attributes row.names and are read with read.table or read.csv)
  • Matrix- filled column-wise
  • Matrix transpose is the function t.
  • Factors - categorical data
  • Functions - are objects
  • R objects can have names:  x <- 1:3   names(x) <- c("George", "Bill", "Barack")
  • Time series objects: have time and secondary structure.

Functions for generating vectors and other structures:
  • Empty vector:  vector(), vector("numeric", length=5)
  • Concatenate: c(0, 6, -2, 3)   
  • Range:     3:10
  • Sequence:   seq(-5, 5, by=0.2)  or  seq(length=11, from=-5)
  • Repetition: rep(1:3, times=5) or rep(2:3, each=5)
  • Empty matrix: matrix(nrow=2, ncol=3) 
  • Vector x to matrix:  matrix(x, 2) or matrix(x, 2, byrow=TRUE)
  • Data frame x to matrix:  as.matrix(x)
  • Binding:  cbind(1:3, 10:12)   or  rbind(1:3, 10:12)
  • List:   list(23.0, "a", FALSE, 2+3i)
Useful plots:
  • Plot:   plot
  • Boxplot:   boxplot
  • Histogram:  hist
  • Quantile plots:  qqnorm
  • Bar chart:  barplot
  • Histogram:  hist
Data frames:
  • Used to represent tables
  • A list - in which each element has same length --- the elements are lists
  • Data frames can have row.names attribute
  • Can be read using read.table or read.csv
  • Convert to matrix using data.matrix() or as.matrix(x)
Subsetting objects:
  • [ ] always returns objects of same class as original
  • [[ ]] extracts single element of a list or data frame
  • $ used to extract by name
  • drop=FALSE prevents conversion to a vector
  • exact = FALSE minit
Removing missing numbers:
  • complete.cases(a, b)   gives good elements (not missing for vectors a and b) -- also works for data frames
Vectorized operations:
  • y==x
Control operations:
  • for loop:                               for (i in 1:100) { }
  • while loop:                           while(i < 100) { }
  • repeat (needs a break):         repeat { }