#----------------------------------------------
# 0. Assigning values to variables
#
# Things to learn:
#   use <- or = to assign values to variables
#   to view the value of a variable, just type it in
#   there are three basic variable types

x <- 3.0            # numerical
y <- 'hello world'  # character (string)
z <- TRUE           # logical (boolean)

x = 3.0             # can use = instead of <-
y = 'hello world'
z = TRUE


#----------------------------------------------
# 1. A simple function
#
# Things to learn:
#   how to define functions, and use {} to enclose blocks of statements
#   a function is a value, just like any other value
#   if statement
#   return statement
#   functions are variables too


fib <- function(i) {
  if (i<=1) return(1)
  x <- fib(i-1) + fib(i-2)
  x }

g <- fib

g(5)


#---------------------------------------------
# 2. Named and optional arguments to functions
#
# Things to learn:
#   arguments can have default values
#   arguments can have names

inc <- function(x, by=1) x+by  # default of by is 1
inc(10) # use the default by=1
inc(10,2) # use by=2
inc(by=3, 10)  # use by=3, then match x to 10



#----------------------------------------------
# 3. Control structures
#
# Things to learn:
#   lists
#   extracting elements from lists; 1-based indexing
#   length
#   while loop
#   print statement

arr <- list(2,4,4,1,0,4)
queuesize <- 0
servicerate <- 3
i <- 1
while (i<=length(arr)) {
  queuesize <- max(queuesize+arr[[i]]-servicerate, 0)
  print(queuesize)
  i <- i+1 }


#----------------------------------------------
# 4. Call by value
#
# Things to learn:
#   semicolon, for sequences of commands on the same line
#   functions can't modify their arguments

removefirst <- function(x) { x[[1]] <- NULL; x }
mylist <- list('hello','world')
removefirst(mylist)           # returns the value list('world')
mylist                        # hasn't actually been changed

mylist <- removefirst(mylist) # use this to change mylist


#----------------------------------------------
# 5. Everything returns a value
#
# Things to learn:
#   if returns a value
#   functions for string manipulation (substr, paste)

x <- 'hello world'
substr(x,1,5)

if (substr(x,1,5)=='hello') 'hello' else 'goodbye'

paste(if(substr(x,1,5)=='hi') 'hello' else 'goodbye', 'to you')


#----------------------------------------------
# 6. Vectorization
#
# Things to learn:
#   how to create a range with :
#   use c to concatenate elements or vectors
#   operations like + work on vectors
#   vector recycling rule
#   ifelse

x <- 1:5
c(1,2,3,4) + c(3,3,2,2)
c(1,2,3,4) + 5  # Vector recycling rule c(1,2,3,4) + c(5,5,5,5)

iseven <- function(x) (x %% 2)==0 
iseven(1:5)
# 1:5 produces c(1,2,3,4,5)
# 1:5 %% 2 produces (1,0,1,0,1)
# (1,0,1,0,1)==0 produces (FALSE,TRUE,FALSE,TRUE,FALSE)

ifelse(iseven(1:5),
       c('happy','cheerful','bright','sunny','fun'),
       c('bad','down','glum','grim','sad'))



#----------------------------------------------
# 7. Indexing by integer and logical
#
# Things to learn:
#   index by integer and vector of integers
#   index by vector of logicals
#   the seq command
#   assignment into vectors

x <- c('bad','cheerful','glum','sunny','sad')
x[c(2,2,1)]  # Integer indexing

x[c(TRUE,FALSE,TRUE,TRUE,FALSE)]  # Logical indexing

x[c(1,3)] <- 'ok'  # assign a value into several elements of the vector
x[c(1,3)] <- c('good','great')  # assign different values into elements 1 and 3

# More logical indexing
x <- seq(1,25,by=3)
x

x[iseven(x)]      # logical indexing; pick out all the even elements
x[iseven(x)] + 1  # pick out even elements, and add 1
x[iseven(x)] <- x[iseven(x)] + 1  # add 1 to all even elements
x


#-------------------------------------------
# 8. Indexing by name
#
# Things to learn:
#   elements in a vector can have names
#   pick out elements by name
#   name() to get all names, and names()<- to assign all names


scores <- c(pat1='muchworse',pat2='worse',pat3='better',pat4='worse')
scores # a named vector

names(scores) # returns c('pat1','pat2','pat3','pat4')

scores['pat2']

categorize <- c('muchworse'=-1, 'worse'=-1, 'same'=0, 'better'=1, 'muchbetter'=1)
categorize[c('worse','better','worse')]

x <- categorize[scores]  # c(-1,-1,1,-1)
names(x) <- names(scores) # assign names(scores) to be the names of x
x



#---------------------------------------------------------
# 9. QUIZ

x <- c('I','am','a','vector')
paste(x,'!')
x[3]
x[c(2,1,4)]


y <- list(x,paste(x,'!',sep=''),sin)
paste(y,'!') # paste is a vectorized function; it converts y into a character vector
y[[1]] # extracts the first element (i.e. gives a vector)
y[c(2,3)] # extract a sublist consisting of 2nd and 3rd elements (i.e. gives a list)
y[1] # a single-element list; the element is a vector of length 4
y[[1]][2] # 'am'
y[1][2] # out of bounds; return NULL

names(y) <- c('vec1','vec2','fun')
y

y$vec2

y[[2]]