###################################################
### chunk number 1: options
###################################################
options(width=65, useFancyQuotes=FALSE)


###################################################
### chunk number 2: first-session
###################################################
sessionInfo()


###################################################
### chunk number 3: libhelp eval=FALSE
###################################################
## library(help=stats)


###################################################
### chunk number 4: help eval=FALSE
###################################################
## ?fivenum


###################################################
### chunk number 5: lattice
###################################################
library(lattice)


###################################################
### chunk number 6: quit eval=FALSE
###################################################
## q()


###################################################
### chunk number 7: setwd eval=FALSE
###################################################
## setwd("~/sharedrsrc/presentations/RIntro")


###################################################
### chunk number 8: list.files eval=FALSE
###################################################
## list.files("extdata", full.names=TRUE)


###################################################
### chunk number 9: weights
###################################################
weights <- read.csv("extdata/weights.csv", header=TRUE, row.names=1)


###################################################
### chunk number 10: class
###################################################
class(weights)


###################################################
### chunk number 11: head
###################################################
head(weights)
dim(weights)


###################################################
### chunk number 12: summary
###################################################
summary(weights)


###################################################
### chunk number 13: colselect
###################################################
class(weights$Weight)
class(weights[["Weight"]])


###################################################
### chunk number 14: Weight-detail
###################################################
weights[["Weight"]]


###################################################
### chunk number 15: log1
###################################################
log(1 + weights[["Weight"]])


###################################################
### chunk number 16: factor-class
###################################################
weights[["Group"]]
class(weights[["Group"]])


###################################################
### chunk number 17: levels
###################################################
levels(weights[["Group"]])


###################################################
### chunk number 18: table
###################################################
table(weights[["Group"]])


###################################################
### chunk number 19: sapply
###################################################
sapply(weights, class)


###################################################
### chunk number 20: str
###################################################
str(weights)


###################################################
### chunk number 21: plot eval=FALSE
###################################################
## plot(weights[["Group"]], weights[["Weight"]])


###################################################
### chunk number 22: lattice
###################################################
library(lattice)


###################################################
### chunk number 23: search
###################################################
search()


###################################################
### chunk number 24: bwplot
###################################################
print(bwplot(Weight ~ Group, weights))


###################################################
### chunk number 25: stripplot
###################################################
print(stripplot(Weight ~ Group, weights, jitter=TRUE))


###################################################
### chunk number 26: tapply
###################################################
tapply(weights[["Weight"]], weights[["Group"]], mean)


###################################################
### chunk number 27: lm1
###################################################
lm(Weight ~ Group, weights)


###################################################
### chunk number 28: lm2
###################################################
fit <- lm(Weight ~ Group - 1, weights)
fit


###################################################
### chunk number 29: anova
###################################################
summary(fit)
anova(fit)


###################################################
### chunk number 30: resid
###################################################
weights[["Residuals"]] <- resid(fit)
weights[["Fitted"]] <- fitted(fit)
head(weights)


###################################################
### chunk number 31: save eval=FALSE
###################################################
## save(weights, fit, file=file.path("data", "weightsAndFits.rda"))


###################################################
### chunk number 32: load eval=FALSE
###################################################
## load(file=file.path("data", "weightsAndFits.rda"))


###################################################
### chunk number 33: quit eval=FALSE
###################################################
## q(save="yes")


###################################################
### chunk number 34: weights-table
###################################################
cat(noquote(paste(readLines("extdata/weights-table.txt", 5), 
                  collapse="\n")))


###################################################
### chunk number 35: read.delim-soln
###################################################
wtsTable <- read.delim("extdata/weights-table.txt")
head(wtsTable)
summary(wtsTable)


###################################################
### chunk number 36: read.table
###################################################
df1 <- read.table("extdata/weights.csv", header=TRUE, sep=",", row.names=1)
df2 <- read.table("extdata/weights-table.txt", header=TRUE)


###################################################
### chunk number 37: iris
###################################################
data(iris)


###################################################
### chunk number 38: iris-explore eval=FALSE
###################################################
## data(iris)
## head(iris)
## summary(iris)
## splom(iris, pch=20)
## xyplot(Sepal.Width ~ Petal.Width, iris, group=Species)


###################################################
### chunk number 39: princomp
###################################################
ipc <- princomp(~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width, 
                iris)
summary(ipc)


###################################################
### chunk number 40: atomic-constructor
###################################################
numeric(5)


###################################################
### chunk number 41: seq1
###################################################
1:5
seq(1, 10, by = 2)


###################################################
### chunk number 42: c
###################################################
c(1, 2, 3)    # numeric (i.e., real) vector of length 3
c(1L, 2L, 3L) # integer vector, 'L' used to specify integer type
c(1L, 2L, 3)  # coerced to common type: numeric vector
c(12, "A")    # coercion to character vector


###################################################
### chunk number 43: rep
###################################################
rep(c(TRUE, FALSE), each=3)


###################################################
### chunk number 44: character
###################################################
letters
pi


###################################################
### chunk number 45: Inf
###################################################
x <- c(1, Inf, -Inf, NaN, NA)
x
typeof(x)
typeof(c(1L, NA))


###################################################
### chunk number 46: names
###################################################
x <- c(a=1, b=2)
x
names(x)


###################################################
### chunk number 47: coerce
###################################################
as.integer(c(1.41, 3.14))


###################################################
### chunk number 48: matrix
###################################################
matrix(1:12, nrow=3)


###################################################
### chunk number 49: matrix-attributes
###################################################
attributes(matrix(1:12, nrow=3))


###################################################
### chunk number 50: matrix-dimnames
###################################################
matrix(1:12, nrow=3, 
       dimnames=list(MyRows=LETTERS[1:3], MyCols=letters[1:4]))


###################################################
### chunk number 51: list
###################################################
list(alpha=letters[1:4], ints=1:4, m=matrix(1:12, nrow=3))


###################################################
### chunk number 52: data.frame
###################################################
data.frame(alpha=letters[1:4], ints=1:4)


###################################################
### chunk number 53: square
###################################################
square <- function(x) {
    x * x
}


###################################################
### chunk number 54: square-at-work
###################################################
square(1:4)


###################################################
### chunk number 55: sapply-square
###################################################
sapply(list(1:4, 3:1), square)
sapply(sapply(list(1:4, 3:1), square), sum)


###################################################
### chunk number 56: subset
###################################################
letters[c(2:3, 15:17, 18:16)]
letters[25:28]
letters[-c(1:20)]


###################################################
### chunk number 57: subset-names
###################################################
x <- c(a=1, b=2, c=3)
x[c("a", "c")]


###################################################
### chunk number 58: recycle
###################################################
x <- 1:10
x[c(FALSE, FALSE, TRUE)]


###################################################
### chunk number 59: logical-subset
###################################################
x <- c(1, 3, 6:9)
idx <- x %% 3 == 0
idx
x[idx]


###################################################
### chunk number 60: matrix-subset
###################################################
m <- matrix(1:12, nrow=3)
m[1:2, -3]


###################################################
### chunk number 61: matrix-drop
###################################################
m[1,]
m[1,,drop=FALSE]


###################################################
### chunk number 62: 
###################################################
x <- 1:3
x[2] <- 4
x


###################################################
### chunk number 63: pass-by-value
###################################################
x <- 1:3
y <- x    # x and y 'the same', i.e., numerically equal
x[2] <- 4 # change x
x         # x changed
y         # not y


###################################################
### chunk number 64: subset2
###################################################
l <- list(m=matrix(1:12, nrow=3), n=1:5)
l
l["m"]
l[["m"]]


###################################################
### chunk number 65: env
###################################################
env <- new.env()  # create a new environment, see ?new.env
env[["x"]] <- 1   # create an element "x", and assign value 1 to it
env[["x"]]
env_copy <- env   # make a copy of the environment
env[["x"]] <- 2   # change the original
env[["x"]]        # env modified
env_copy[["x"]]   # but so is env_copy!


###################################################
### chunk number 66: 
###################################################
log(1 + sqrt(1:5))
dev <- rnorm(100) # 100 random normal deviates
max(dev)
any(dev > 2)   # is any logical element TRUE?
sum(dev > 2)   # convert logical element to numeric (TRUE==1) and sum
s <- c("Fred", "Frank")
sub("F", "G", s)  # replace F with G in all elements of s


###################################################
### chunk number 67: if
###################################################
dev <- rnorm(100)
if (any(dev > 2)) {
    cat("some deviates greater than 2\n")
} else {
    cat("hmm, all deviates less than 2\n")
}


###################################################
### chunk number 68: for
###################################################
for (i in 2:1) {
    cat("I am", i, "\n")
}
lst <- list(a=1, b=2)
for (elt in lst) {
    cat("I am", elt, "\n")
}


###################################################
### chunk number 69: for-prealloc eval=FALSE
###################################################
## results <- numeric(10000)
## for (i in seq_along(results)) {
##     results[[i]] <- aFancyCalculation()
## }


###################################################
### chunk number 70: sapply
###################################################
cls1 <- sapply(iris, class)


###################################################
### chunk number 71: sapply-for
###################################################
cls2 <- character(ncol(iris))
for (i in seq_along(cls2)) {
    cls2[[i]] <- class(iris[[i]])
}
names(cls2) <- names(iris)
identical(cls1, cls2)


###################################################
### chunk number 72: subset-ex
###################################################
x <- -5:5
x * x
sum(x * x)
sqrt(x)
x %*% x
length(x * x)
x[6]
x[6] <- NA
x * x
sum(x * x)
sum(x * x, na.rm=TRUE)
is.na(x)
y <- x[ ! is.na(x) ]
x[!is.na(x) | (x * x) <= 10]


###################################################
### chunk number 73: square-again eval=FALSE
###################################################
## square <- function(x) {
##     x * x
## }


###################################################
### chunk number 74: functions-ex-solution
###################################################
sumsq <- function(x) {
    sum(x * x)
}
sumsq((-5):5)
sumsq(c(1, 2, NA))
sumsq <- function(x, na.rm=FALSE) {
  sum(x * x, na.rm=na.rm)
}
sumsq(c(1, 2, NA))
sumsq(c(1, 2, NA), na.rm=TRUE)
sumsq(c(1, 2, NA), na.rm=FALSE)
sumsq(na.rm=TRUE, c(1, 2, NA))
try(sumsq(letters))
sumsq <- function(x, na.rm=FALSE) {
  if (!is.numeric(x)) {
     stop("'x' is '", class(x), "' but should be 'numeric'")
  }
  sum(x * x, na.rm=na.rm)
}
try(sumsq(letters))
sumsq(numeric(0))


###################################################
### chunk number 75: script eval=FALSE
###################################################
## source('script.R')


