Here $x$ is a 100 randomly generated data points from the exponential distribution with rate $1$. # Functions Suppose we needed to use the quantity $\sin(x) + \cos(x)$ over and over again for different values of $x$, then it may be useful to define this as a function in the following way: ```{r} sincos <- function(x){ z <- sin(x) + cos(x); z } sincos(1) ```

Here the function sincos takes an input $x$. It is sometimes useful have functions that do not take inputs, but simple perform operations: say roll a fair dice 1o times, and take the sum. # Basic programming in R We will introduce basic programming in R with the following exercise and will illustrate how to write a *while* loop in R.

```{exercise} By running simulations in R, approximate the average number of rolls of a fair dice it takes before you see a 6. ```

```{solution} We make a function *numrolls* which counts how many times we need to roll a dice until we see a $6$. Then we use the *replicate* command to repeat this function many time, and take the average. ```

```{r} numrolls <- function(){ n=0 x=0 while(x <6){ x <- sample(6,1, replace =TRUE) n <- n+1 } n } mean(replicate(1000, numrolls()) ) ``` In the next exercise, we illustrate how to write a *for* loop. ```{exercise} Define a function that tells you whether a positive integer is prime or not. ```

```{solution} Let $n$ be an integer. We first recall that $d$ is a divisor of $n$ if there exists an integer $c$ such that $ n = cd$. An integer $n \geq 2$ is prime if it only divisors are $1$ and $n$. R has a built in remainder function, which for nonnegative integers $a,b$ outputs the remainder in the sense of elementary school, when $a$ is divided by $b$. Using the remainder function we define the *isprime* function, and use it spit out the prime numbers up to 500. ```{r} 25%%5 26%%5 isprime <- function(n){ x=1 for (i in 2: (n-1)){ if (x >0) { x <- n%%i } } if (n==1) {x <-0} if (n==2) {x <-1} x } isprime(1) isprime(2) isprime(101) x=2 for(i in 3:500){ if( isprime(i)==1){ x <- c(x, i)} x } x ``` # Summary We introduced some basics of R, and we gave examples programming basics including defining functions, while loops, for loops, and using the replicate function.

# Version: `r format(Sys.time(), '%d %B %Y')` * [Rmd Source](https://tsoo-math.github.io/ucl/intro-R.Rmd)