R workshop: day #1

Mike Hammond
U. of Arizona
  1. General
    1. q() — quit the program.
    2. control-c — stop R if it's doing something you don't want it to.
    3. apropos('some topic') — find out about some topic (must be quoted).
    4. help(command) — find out about some command.
    5. ?command — same as above.
    6. dir() — list the contents of the current directory.
    7. getwd() — get the current working directory.
    8. setwd() — set the current working directory (or use the Misc menu in the GUI).
  2. Vectorized calculating
    1. Simple math
      2 + 3
      6 / 2
      7 * 34
      8^3
      sum(2,7,3)
      
    2. Variables
      a <- 4
      a
      2 * a
      b <- 7
      b + a
      
    3. Vectors
      c(3,6,27,2)
      c(4,9,4)^2
      a <- c(4,9,4)
      a / c(2,5,4)
      a / c(2,5)
      b <- 2:8
      c(a,b)
      c('a','b')
      
  3. Importing data
    1. data.frame() — a vector of vectors, the standard format for data for statistical analysis. (All the vectors must be of the same length.)
    2. You can create a dataframe by typing it in at the prompt.
      age <- c(25,30,56,22,17,9)
      gender <- c("m","f","m","m","f","f")
      weight <- c(160,110,220,150,90,100)
      major <- c('ling','comm','comm','ling','ling','ling')
      g <- data.frame(age,gender,weight,major)
      
      Notice that words or strings must be quoted; single or double quotes will work.
    3. fix() — alter a statistical object by typing it in with the R editor.
      g <- data.frame(age=numeric(0), gender=character(0),
         weight=numeric(0), major=character(0))
      fix(g)
      
    4. read.table() — read in a text file and make a dataframe object from it.
      g <- read.table('gender.txt',header=T)
      
      Note that the file must be a real text file and must be located in the current working directory.