########################################################################
##
## Gj: 12-Apr-2000
##
## Splus commands used for Short Course on Splus, part 1.
##
########################################################################

## basic expressions and assignments...

## expressions
4 + 5           ## addition
(9 * 5) -   7   ## use parentheses
(8/2)^2         ## power

## assignments:
x <- 45                    ## assigning 45 to x
x                          ## the object x
mu <- 40; s <- 10          ## mean and SD
z <- (x - mu)/s            ## Z-value
z
lik <- 1/sqrt(2*s^2) *
  exp(-0.5 * z^2)          ## the normal density
lik
log.lik <- log(lik)        ## the log-likelihood 
log.lik

ls()
rm(log.lik)

########################################################################
## more on S:

## vectors:
data <- c(25,31,27,29,26)   ## concatenating
data                        ## print data
length(data)                ## the length
data[3]                     ## element 3
ind <- 2:4                  ## sequence (indexing)
ind
data[ind]                   ## subset
log(data)                   ## logarithm
sum(data)                   ## sum
mean(data)                  ## mean
sqrt(var(data))             ## SD
stdev(data)                 ## SD also
lev <- c("high","low")      ## characters
lev[1]

## lists:
my.l <- list(x=1:5, y=data)
my.l             ## print out the list
names(my.l)      ## the names (attributes)
my.l$x           ## the x item of the list
my.l$y[2:4]      ## elements 2-4 of y

## matrices:
mat <- cbind(x=1:5, y=rnorm(5,mean=10,sd=5))
mat
dim(mat)            ## the dimension
dimnames(mat)       ## row and column names, if any
mat[1:2,]           ## first two rows
mat[,1]             ## the first column, by number
mat[,'y']           ## the second column, by name
mat.2 <- mat[-1,]   ## remove the first line
mat.2

## use logicals:
ind <- mat[,'y'] > 10  ## which 'y' are > 10
ind  ## vector of logicals, TRUE/FALSE
mat.3 <- mat[ind,]  ## store in a separate matrix
mat.3

mat[!((mat[,'y'] > 8) & (mat[,'y'] < 12)),]


########################################################################

## The toxicity data:

## Import:
tox <- read.table("/home/gardar/pub/toxicity.dat",
                  header=T)
tox
class(tox)
names(tox)
tox$con
names(tox) <- c('d','n','r')
tox[1:2,]

## Explore:
summary(tox)   ## summary info for every col
sapply(tox,sum)   ## apply sum of every col
res <- tox$n - mean(tox$n)  ## deviation from mean
summary(res)

## Mange:
tox$p <- tox$r/tox$n * 100   ## percentage killed
tox
tox[tox$p > 50,]
tox$p/tox$d   ## % killed per toxicity

