---
title: "Week 3 Lecture 1"
output: html_document
---

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

### Review of PS1

We reviewed the solutions [http://guerzhoy.mycpanel.princeton.edu/201s19/pre/P1/p1_soln.html](for PS1).


### $ vs. select

The `$` operator allows us to extract a column from a table, as a vector:

```{r}
gapminder$continent[1:100]  # outputting the first 100 elements so as not to waste space
```

We output just the first 100 elements of the vector. We could use `gapminder$continent` to get all of them.

On the other hand, if we select one column using `select`, we will still get a data frame:

```{r}
gapminder %>% select(continent)
```

(Note that when a data frame is output, not all the rows are printed by default, so we did not need to artificially restrict the number of rows outputted like we did with the vector.)


### `sapply`, again

Here is the example we saw last time:

```{r}
special_square <- function(x){
  if(x == 42){
    return(0)
  }else{
    return(x**2)
  }
}
```

We couldn't use `special_square(c(1, 2, 42, 3))` because `special_square` contains an if-statement involving `x`. So instead, we used

```{r}
sapply(c(1, 2, 42, 3), FUN = special_square)
```

Now, consider a slightly more complicated situation:

```{r}
mult <- function(x, y){
  return(x * y)
}
```

The function `mult` takes two arguments. Suppose we want to multiply every element of `c(1, 2, 3)` by `7`. Here is how we can do it:

```{r}
v <- c(1, 2, 3)
z <- 7
sapply(v, FUN = mult, z)
```

The result is multiplying `v[1]`, `v[2]`, and `v[3]` by `z`. The first argument is the vector each of whose elements we want to pass to the function. After the `FUN` argument, we can plug in other arguments that would go to `mult`. In our case, `z` gets assigned to the parameter `y`.


### The bug in the OKCupid dataset analysis

Consider this code from last lecture.

    ContainsWord <- function(query, text){
      return(length(grep(query, text)) > 0)
    }
    profile.aug <- profiles %>% mutate(contains.love = sapply(profiles$essay0, FUN=ContainsWord, "love"))`


Running this code results in an error. The reason is that we are passing each of the elements of the vector `profiles$essay0` into `query`, and we are passing `"love"` into `text`. The reason this happens is that elements of the first argument of `sapply` will go to the first parameter of `ContainsWord`.

But `grep(profiles$essay0[0], "love")` makes no sense: we want to look for the pattern `"love"` in the essay, not the other way around. One solution is to simply switch the paramters `query` and `text` in `function(query, text)`. Then, when we are running `grep`, we'll be doing something that makes sense.

```{r}
library(okcupiddata)

ContainsWord <- function(text, query){
  return(length(grep(query, text)) > 0)
}

profile.aug <- profiles %>% mutate(contains.love = sapply(profiles$essay0, FUN=ContainsWord, "love"))

profile.aug %>% group_by(income) %>% 
  summarize(love.prop = mean(contains.love))

```
