JoyofRexample1.R

xn8 — Aug 14, 2013, 4:41 PM

################## DATA #####################

# Data types

aNumber <- 4
aNumber
[1] 4
anotherNumber <- ((3*aNumber) + sqrt(4))/aNumber
anotherNumber
[1] 3.5
aCharacter <- "4"
aCharacter
[1] "4"
aLogical <- TRUE
aLogical
[1] TRUE

# Simple data structures

aLogicalVector <- c(TRUE, FALSE, FALSE, FALSE)
aLogicalVector
[1]  TRUE FALSE FALSE FALSE

aCharacterVector <- c("t", "f", "t")
aCharacterVector
[1] "t" "f" "t"

aFactorVariable <- as.factor(aCharacterVector)
aFactorVariable
[1] t f t
Levels: f t

aNumericVector <- seq(1:31)
anotherNumericVector <- rep(1:7,4)
aSample <- sample(aNumericVector,5)
aSample
[1] 13 21 22  7 11
anotherNumericVector[22] # Index to pull one element of vector
[1] 1

aNumericMatrix <- matrix(aNumericVector, ncol=7, byrow=TRUE)
Warning: data length [31] is not a sub-multiple or multiple of the number
of rows [5]
aNumericMatrix
     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,]    1    2    3    4    5    6    7
[2,]    8    9   10   11   12   13   14
[3,]   15   16   17   18   19   20   21
[4,]   22   23   24   25   26   27   28
[5,]   29   30   31    1    2    3    4
aNumericMatrix[3,4]  # Index to pull one element of matrix
[1] 18

# Complex data structures

alist <- list(c("a", "b", "c"), c(1,2,3,4), c(TRUE, FALSE))
a <- alist[[2]]
b <- alist[2]
alist[[2]][2]
[1] 2
alist[2][2]
[[1]]
NULL

employee <- c('John Doe','Robin Miller','Julie Smith')
salary <- c(21000, 23400, 26800)
startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14'))
aDataframe <- data.frame(employee,salary,startdate)
aDataframe
      employee salary  startdate
1     John Doe  21000 2010-11-01
2 Robin Miller  23400 2008-03-25
3  Julie Smith  26800 2007-03-14
aDataframe[2,3] # Indexing a dataframe element
[1] "2008-03-25"
aDataframe[2,] # Indexing a dataframe row
      employee salary  startdate
2 Robin Miller  23400 2008-03-25
aDataframe[,2] # Indexing a dataframe column
[1] 21000 23400 26800

# Reading in Data
data(mtcars)  # Built-in dataframe
tragedy<-read.csv(file="tragedies.csv") # Dataframe from file

################## TECHNIQUES #####################

# Conditionals

if (aNumber < 10) {
  print (paste(aNumber,"is less than 10"))
}
[1] "4 is less than 10"

# Iteration
for (i in 1:nrow(aDataframe)) {
  print (aDataframe[i,2] * 1.03)
}
[1] 21630
[1] 24102
[1] 27604

# Implied iteration
raises <- aDataframe[,2]*1.03
raises
[1] 21630 24102 27604
ifelse(anotherNumericVector > 3, "more", "less or equal")
 [1] "less or equal" "less or equal" "less or equal" "more"         
 [5] "more"          "more"          "more"          "less or equal"
 [9] "less or equal" "less or equal" "more"          "more"         
[13] "more"          "more"          "less or equal" "less or equal"
[17] "less or equal" "more"          "more"          "more"         
[21] "more"          "less or equal" "less or equal" "less or equal"
[25] "more"          "more"          "more"          "more"         


# Function
compareVector <- function (numbers, compareto) {
  for (i in 1:length(numbers)) {
    if (numbers[i] > compareto){
      print (paste(i, "is more than ", compareto))
    }
    else {
      print (paste(i,"is less than or equal to ", compareto))
    }
  }
}
compareVector(anotherNumericVector,  3) # Function call
[1] "1 is less than or equal to  3"
[1] "2 is less than or equal to  3"
[1] "3 is less than or equal to  3"
[1] "4 is more than  3"
[1] "5 is more than  3"
[1] "6 is more than  3"
[1] "7 is more than  3"
[1] "8 is less than or equal to  3"
[1] "9 is less than or equal to  3"
[1] "10 is less than or equal to  3"
[1] "11 is more than  3"
[1] "12 is more than  3"
[1] "13 is more than  3"
[1] "14 is more than  3"
[1] "15 is less than or equal to  3"
[1] "16 is less than or equal to  3"
[1] "17 is less than or equal to  3"
[1] "18 is more than  3"
[1] "19 is more than  3"
[1] "20 is more than  3"
[1] "21 is more than  3"
[1] "22 is less than or equal to  3"
[1] "23 is less than or equal to  3"
[1] "24 is less than or equal to  3"
[1] "25 is more than  3"
[1] "26 is more than  3"
[1] "27 is more than  3"
[1] "28 is more than  3"

# Built in functions for analysis and output

mean(aDataframe$salary)
[1] 23733
plot(tragedy$Word.Count ~ tragedy$Author)

plot of chunk unnamed-chunk-1


################## TOOLS #####################  

# Tools & Techniques used in this exercise

# - RStudio Projects
# - Locating help
# - Keyboard shortcuts: command completion with tab, up-arrow
# - Workspace and history
# - Creating a script 
# - Creating & publishing an RStudio Notebook