###################################################
### chunk number 1: setup0
###################################################
options(width=65)


###################################################
### chunk number 2: vectors
###################################################
v <- 123
s <- "a string"
t <- TRUE
letters            # 'letters' is a built-in variable
length(letters)    # 'length' is a function


###################################################
### chunk number 3: vectorsRepAndSeq
###################################################
seq(1, 3) 
1:3
rep(1:2, 3)
vector(mode="character", length=5)


###################################################
### chunk number 4: matrixEx
###################################################
x <- matrix(1:10, nrow=2)
dim(x)
x
as.vector(x)


###################################################
### chunk number 5: names1
###################################################
x <- c(a=0, b=2)
x
names(x) <- c("Australia", "Brazil")
x


###################################################
### chunk number 6: names2
###################################################
x <- matrix(c(4, 8, 5, 6, 4, 2, 1, 5, 7), nrow=3)
dimnames(x) <- list(
  year = c("2005", "2006", "2007"), 
  "mode of transport" = c("plane", "bus", "boat"))
x


###################################################
### chunk number 7: posind
###################################################
x <- 1:10
x[2]
x[1:3]


###################################################
### chunk number 8: posind2
###################################################
 x[9:11]


###################################################
### chunk number 9: posind3
###################################################
x[0:1]
x[c(0, 0, 0)]


###################################################
### chunk number 10: posind4
###################################################
x[c(10, 2, NA)]



###################################################
### chunk number 11: posassign
###################################################
x[2] <- 200
x[8:10] <- 10
x


###################################################
### chunk number 12: negsub
###################################################
x[-(1:3)]


###################################################
### chunk number 13: negassignb
###################################################
x = 1:10
x[-(8:10)] = 10
x



###################################################
### chunk number 14: logicalsub
###################################################
x = 1:10
x > 5
x[x > 5]


###################################################
### chunk number 15: namedsub
###################################################
x <- c(a=1, b=2, c=3)
x[c("c", "a", "foo")]



###################################################
### chunk number 16: arraysub
###################################################
 x = matrix(1:9, ncol=3)
 x[ x > 6 ]
 x[row(x) > col(x)] = 0
 x



###################################################
### chunk number 17: vec1
###################################################
1:3 + 10:12


###################################################
### chunk number 18: vec2
###################################################
 1 + 1:5
 paste(1:5, "A", sep="")


###################################################
### chunk number 19: listenv
###################################################
 lst = list(a=1:3, b = "ciao", c = sqrt)
 lst
 lst$c(81)


###################################################
### chunk number 20: env1
###################################################
e1 = new.env()
e1[["a"]] <- 1:3
assign("b", "ciao", e1)
ls(e1)


###################################################
### chunk number 21: 
###################################################
 lst[1]


###################################################
### chunk number 22: 
###################################################
 lst[[1]]


###################################################
### chunk number 23: 
###################################################
 lst$a
 lst[["a"]]


###################################################
### chunk number 24: 
###################################################
 mget(c("a", "b"), e1)


###################################################
### chunk number 25: 
###################################################
 e1$a
 e1[["b"]]


###################################################
### chunk number 26: 
###################################################
 lst[[1]] = list(2,3)
 lst[[1]]

 e1$b = 1:10
 e1$b


###################################################
### chunk number 27: dataFrameEx
###################################################
df <- data.frame(type=rep(c("case", "control"), c(2, 3)), time=rexp(5))
df
df$time
names(df)
rn <- paste("id", 1:5, sep="")
rownames(df) <- rn
df[1:2, ]


###################################################
### chunk number 28:  eval=FALSE
###################################################
##  source("http://bioconductor.org/biocLite.R")
##  biocLite("Biobase")


###################################################
### chunk number 29:  eval=FALSE
###################################################
##  library(Biobase)


###################################################
### chunk number 30: pasteExample
###################################################
as.roman(1:10)
paste("chr", as.roman(1:5), sep="")
paste("chr", as.roman(1:5), sep="", collapse=", ")
grep("IV", as.roman(1:30))
grep("I[VX]", as.roman(1:30), value=TRUE)


###################################################
### chunk number 31: apply1
###################################################
a = matrix(runif(1e6), ncol=10)
## 'apply'
s1 = apply(a, 1, sum)

## 'for', pre-allocating for efficiency
s2 = numeric(nrow(a))
for(i in 1:nrow(a))
  s2[i] = sum(a[i,])

## purpose-built function (must faster!)
s3 = rowSums(a)


###################################################
### chunk number 32: sqfun
###################################################

 square = function(x) x*x
 square(10)


###################################################
### chunk number 33: sf2
###################################################
 square(1:4)


###################################################
### chunk number 34: callsumsq
###################################################
 sumsq = function(x) sum(square(x))
 sumsq(1:10)


###################################################
### chunk number 35: classEx1
###################################################

setClass("Experiment",
    representation(assay="matrix", phenotype="data.frame"))

setMethod(show, signature(object="Experiment"),
    function(object) 
{
    cat("class: ", class(object), "\n")
    cat("assay dimensions: ", dim(object@assay), "\n")
    cat("phenotype names: ", names(object@phenotype), "\n")
})
x <- new("Experiment", assay=matrix(rnorm(100), ncol=5),
         phenotype=data.frame(Gender=sample(c("M", "F"), 5, TRUE),
                              Age=30 + ceiling(10 * runif(5))))
show(x)