A Report Generated by knitr

This report is automatically generated with the R package knitr (version `1.5`) .

    # Chapter 4 - Getting Started with Arithmetic Working with Numbers, Infinity, and Missing
    # Values Doing basic arithmetic Using arithmetic operators
    baskets.of.Granny <- c(12, 4, 4, 6, 9, 3)
    baskets.of.Geraldine <- c(5, 3, 2, 2, 12, 9)
    Granny.money <- baskets.of.Granny * 120
    Geraldine.money <- baskets.of.Geraldine * 145
    Granny.money + Geraldine.money

    ## [1] 2165  915  770 1010 2820 1665

    baskets.of.Granny * 120 + baskets.of.Geraldine * 145

    ## [1] 2165  915  770 1010 2820 1665

    ### Controlling the order of the operations
    4 + 2 * 3

    ## [1] 10

    (4 + 2) * 3

    ## [1] 18

    ## Using mathematical functions Calculating logarithms and exponentials
    log(1:3)

    ## [1] 0.0000 0.6931 1.0986

    log(1:3, base = 6)

    ## [1] 0.0000 0.3869 0.6131

    x <- log(1:3)
    exp(x)

    ## [1] 1 2 3

    ### Putting the science in scientific notation
    13300

    ## [1] 13300

    0.0412

    ## [1] 0.0412

    1200000/2000

    ## [1] 600

    ### Rounding numbers
    round(123.456, digits = 2)

    ## [1] 123.5

    round(-123.456, digits = -2)

    ## [1] -100

    signif(-123.456, digits = 4)

    ## [1] -123.5

    ### Using trigonometric functions
    cos(120)

    ## [1] 0.8142

    cos(120 * pi/180)

    ## [1] -0.5

    ## Calculating whole vectors
    2 + 3

    ## [1] 5

    ## To infinity and beyond Using infinity
    2/0

    ## [1] Inf

    4 - Inf

    ## [1] -Inf

    is.finite(10^(305:310))

    ## [1]  TRUE  TRUE  TRUE  TRUE FALSE FALSE

    ### Dealing with undefined outcomes
    Inf/Inf

    ## [1] NaN

    NaN + 4

    ## [1] NaN

    ### Dealing with missing values
    x <- NA
    x + 4

    ## [1] NA

    log(x)

    ## [1] NA

    is.na(x)

    ## [1] TRUE

    ### Calculating infinite, undefined, and missing values Organizing Data in Vectors
    ### Discovering the properties of vectors Looking at the structure of a vector
    str(baskets.of.Granny)

    ##  num [1:6] 12 4 4 6 9 3

    length(baskets.of.Granny)

    ## [1] 6

    authors <- c("Andrie", "Joris")
    str(authors)

    ##  chr [1:2] "Andrie" "Joris"

    ### Testing vector types
    is.numeric(baskets.of.Granny)

    ## [1] TRUE

    is.integer(baskets.of.Granny)

    ## [1] FALSE

    x <- c(4L, 6L)
    is.integer(x)

    ## [1] TRUE

    ## Creating vectors seq(from = 4.5, to = 2.5, by = -0.5) seq(from = -2.7, to = 1.3,
    ## length.out = 9)
    baskets.of.Granny <- c(12, 4, 4, 6, 9, 3)
    baskets.of.Geraldine <- c(5, 3, 2, 2, 12, 9)
    ## Combining vectors
    all.baskets <- c(baskets.of.Granny, baskets.of.Geraldine)
    all.baskets

    ##  [1] 12  4  4  6  9  3  5  3  2  2 12  9

    ## Repeating vectors
    rep(c(0, 0, 7), times = 3)

    ## [1] 0 0 7 0 0 7 0 0 7

    rep(c(2, 4, 2), each = 3)

    ## [1] 2 2 2 4 4 4 2 2 2

    rep(c(0, 7), times = c(4, 2))

    ## [1] 0 0 0 0 7 7

    rep(1:3, length.out = 7)

    ## [1] 1 2 3 1 2 3 1

    # Getting Values in and out of Vectors Understanding indexing in R
    numbers <- 30:1
    numbers

    ##  [1] 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10  9  8  7  6  5  4  3
    ## [29]  2  1

    ## Extracting values from a vector
    numbers[5]

    ## [1] 26

    numbers[c(5, 11, 3)]

    ## [1] 26 20 28

    indices <- c(5, 11, 3)
    numbers[indices]

    ## [1] 26 20 28

    numbers[-3]

    ##  [1] 30 29 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10  9  8  7  6  5  4  3  2
    ## [29]  1

    numbers[-(1:20)]

    ##  [1] 10  9  8  7  6  5  4  3  2  1

    # numbers[-1:20] # NOT RUN, gives error Changing values in a vector
    baskets.of.Granny[3] <- 5
    baskets.of.Granny

    ## [1] 12  4  5  6  9  3

    baskets.of.Geraldine[c(2, 4)] <- 4
    baskets.of.Geraldine

    ## [1]  5  4  2  4 12  9

    Granny.copy <- baskets.of.Granny
    baskets.of.Granny[4] <- 11
    baskets.of.Granny

    ## [1] 12  4  5 11  9  3

    baskets.of.Granny <- Granny.copy
    baskets.of.Granny

    ## [1] 12  4  5  6  9  3

    # Working with Logical Vectors Comparing values
    baskets.of.Granny > 5

    ## [1]  TRUE FALSE FALSE  TRUE  TRUE FALSE

    which(baskets.of.Granny > 5)

    ## [1] 1 4 5

    the.best <- baskets.of.Geraldine < baskets.of.Granny
    which(the.best)

    ## [1] 1 3 4

    ## Using logical vectors as indices
    baskets.of.Granny[the.best]

    ## [1] 12  5  6

    x <- c(3, 6, 1, NA, 2)
    x[x > 2]

    ## [1]  3  6 NA

    x > 2

    ## [1]  TRUE  TRUE FALSE    NA FALSE

    ## Combining logical statements
    min.baskets <- baskets.of.Granny == min(baskets.of.Granny)
    max.baskets <- baskets.of.Granny == max(baskets.of.Granny)
    min.baskets | max.baskets

    ## [1]  TRUE FALSE FALSE FALSE FALSE  TRUE

    x[!is.na(x)]

    ## [1] 3 6 1 2

    ## Summarizing logical vectors
    sum(the.best)

    ## [1] 3

    any(the.best)

    ## [1] TRUE

    all(the.best)

    ## [1] FALSE

    # Powering Up Your Math with Vector Functions Using arithmetic vector operations
    # Summarizing a vector
    min(baskets.of.Granny)

    ## [1] 3

    max(baskets.of.Granny)

    ## [1] 12

    sum(baskets.of.Granny, baskets.of.Geraldine)

    ## [1] 75

    x <- c(3, 6, 2, NA, 1)
    sum(x)

    ## [1] NA

    sum(x, na.rm = TRUE)

    ## [1] 12

    ### Cumulating operations
    cumsum(baskets.of.Granny)

    ## [1] 12 16 21 27 36 39

    cummax(baskets.of.Geraldine)

    ## [1]  5  5  5  5 12 12

    cummin(x)

    ## [1]  3  3  2 NA NA

    ### Calculating differences
    diff(baskets.of.Granny)

    ## [1] -8  1  1  3 -6

    diff(x)

    ## [1]  3 -4 NA NA

    ## Recycling arguments
    Granny.pointers <- c(10, 2, 4, 0, 4, 1, 4, 2, 7, 2, 1, 2)
    points <- Granny.pointers * c(2, 3)
    points

    ##  [1] 20  6  8  0  8  3  8  6 14  6  2  6

    sum(points)

    ## [1] 87

    sum(Granny.pointers * c(2, 3))

    ## [1] 87

    round(diff(baskets.of.Granny)/baskets.of.Granny * 100)

    ## Warning: longer object length is not a multiple of shorter object length

    ## [1]  -67   25   20   50  -67 -267

    round(diff(baskets.of.Granny)/baskets.of.Granny[1:5] * 100)

    ## [1] -67  25  20  50 -67

The R session information (including the OS info, R version and all packages used):

    sessionInfo()

    ## R version 3.0.2 (2013-09-25)
    ## Platform: x86_64-w64-mingw32/x64 (64-bit)
    ##
    ## locale:
    ## [1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252
    ## [3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
    ## [5] LC_TIME=English_United Kingdom.1252
    ##
    ## attached base packages:
    ## [1] stats     graphics  grDevices utils     datasets  methods   base
    ##
    ## other attached packages:
    ##  [1] BiocInstaller_1.12.1 ggplot2_0.9.3.1      reshape2_1.2.2       sos_1.3-8
    ##  [5] brew_1.0-6           stringr_0.6.2        knitr_1.5            plyr_1.8
    ##  [9] Revobase_7.1.0       RevoMods_7.1.0       RevoScaleR_7.1.0     lattice_0.20-27
    ## [13] rpart_4.1-2
    ##
    ## loaded via a namespace (and not attached):
    ##  [1] codetools_0.2-8    colorspace_1.2-4   dichromat_2.0-0    digest_0.6.4
    ##  [5] evaluate_0.5.1     foreach_1.4.1      formatR_0.10       fortunes_1.5-2
    ##  [9] grid_3.0.2         gtable_0.1.2       highr_0.3          iterators_1.0.6
    ## [13] labeling_0.2       MASS_7.3-29        munsell_0.4.2      proto_0.3-10
    ## [17] RColorBrewer_1.0-5 scales_0.2.3       tools_3.0.2

    Sys.time()

    ## [1] "2014-05-13 15:05:29 BST"