---
title: "SML201 Week 1 Lecture 1: Introduction to R"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## Evaluating R expressions

First, let's evaluate some R expressions in the console. The simplest expressions are numerics and strings:

```{r}
42
```

```{r}
"Hello"
```

R simply repeats these values back at us.

A more complex expression is something like `42 + 43`. Let's try this:
```{r}
42 + 43
```

This is more interesting -- R **evaluated** the expression and gave us the value back. We can evaluate more complex expressions too:

```{r}
(45 - 43) ** 3
```

We can also evaluate logical conditions
```{r}
5 == (4 + 1)
```

```{r}
7 > (5 - 1)
```

Note that to compare to values, we need to use two equal signs: `==`. The resultant value is `True` or `False` (in R, you can also use `T` and `F`). It's a value just like numeric values and character values are values. It is called a `logical` value.

## Printing to the console

We can also print the values of expressions to the console. This is subtly different from just entering expressions and getting their values back:

```{r}
cat(42 + 1)
cat("Hello")
```

Note that `Hello` was printed without quotes -- that's because we are not just printing the value of `"Hello"` back, we are just printing "Hello." (As an aside, in the Philosophy of Language, this is called "disquotation.")

We can print several values like so:

```{r}
cat("Hello", 123, "hi", 5 + 1)
```

## Variables

Variables in R store values. Here is a simple example:

```{r}
exam <- 80
cat(exam)
```

The variable `exam` stores 80 after we execute `exam <- 80`. We can read this as "store 80 in `exam`". Note that `cat(exam)` and `cat("exam")` are different (exercise: explain how they are different).

Let's now look at a larger program

```{r}
exam <- 80
harvard_adj <- 10
exam <- exam + harvard_adj
cat("Your exam grade is", exam)
```
n <- 125
if(n >= 0){
  cat(n)
} else {
  cat(-n)
} 

Note how R variables are different from the variables you're used to in math: in math, you cannot say `x = x + y` (or rather, you can, but this will just mean that `y = 0`). In R, the similar expression `exam <- exam + harvard_adj` means 
* Compute `exam + harvard_adj`
* Store the resultant value back in the variable `exam`


## Conditionals

Here is how we can compute the absolute value of any number

```{r}
n <- 125
if(n >= 0){
  cat(n)
} else {
  cat(-n)
} 
```

(Note that we could assign any other numeric value to `n`).

The `if` statement above works by first evaluating `n >= 0`. If the value is `True`, `cat(n)` is excuted (the statement(s) inside the `if`-clause). Otherwise, the statment inside the `else`-clause is executed. That means that for positive numbers, we'll print the number itself. For negative numbers, we'll print the positive number with the same aboslute value. So in either case, we'll print the absolute value of `n`.

Here is another example:

```{r}
exam_grade <- 45
if(exam_grade == 98){
  cat("I am reasonably happy")
}else if(exam_grade >= 60){
  cat("Hooray! I passed!")
  cat("But I could do better")
}else{
  cat("Alas")
}
```

This demonstrates the fuller capabilities of the if-statement. Here is what happens: we evaluate the conditions top-to-bottom, so, we evaluate, in order,

* `exam_grade == 98`
* `exam_grade >= 60`

If a condition is true, we execute the statements inside the corresponding clause, and don't execute anything else. If a condition is false, we move on to the next condition. If none of the conditions are true, we execute the statements inside the `else` clause.

Exercise: predict what would be printed for various values of `exam_grade`.

## Functions

Here is an example of a function

```{r}
my_abs <- function(x){
  return(abs(x) + 1)
}
```


We can use (we say: "call") the function like so:

```{r}
cat(my_abs(-5))
cat(my_abs(10))
```

The variable `x` is a "dummy variable" (think $f(x) = x^2$ or $g(y) = y^2$, which are exactly the same, since the names of the functions and the dummy variables don't matter). When we call the function, we assign `-5` (or `10`, or anything else) to `x`. The value of the function is determined by what's inside the `return` statement. 

We store the function (in this instance) in the variable `my_abs`, so that we can then call the function.

Note that a function is just another type of value:

```{r}
my_abs2 <- my_abs
my_abs2(-4)
```

Here, we assigned `my_abs` to a new variable, `my_abs2`, so we can now call the same function using `my_abs2`.


Here is a more elaborate function
```{r}
guess_secret_number <- function(secret_num){
  temp <- secret_num + 8
  temp <- temp * 2
  temp <- temp / 4
  answer <- temp - secret_num / 2
  return(answer)
}
```

Like we saw in class, this function will always return `4` (because of math). The point here is that R can do "scratch work" inside the function. Note that the variable `temp` gets discarded after we finish running the function.

```{r}
cat(guess_secret_number(10))
```

(Try running `cat(temp)` to see that that will produce an error.)
