The purpose of this challenge is to practice writing more efficient code. You will be presented with a series of code chunks that I'm honestly ashamed to have written! Using a combination of vectorized functions and more efficient handling of memory, try to see how fast you can make it run. Be creative!

Here's the dataset we'll be using for this challenge -- a random sample of 5,000 tweets related to the 2014 EP elections in the UK. To make it a bit more challenging, we'll multiply the size of the dataset by 5. We will also make our life easier by removing observations with missing values.

```{r}
d <- read.csv("~/data/UK-tweets.csv", stringsAsFactors=F)
d <- rbind(d, d, d, d, d)
d <- na.omit(d)
```

## 1) Count how many tweets are classified as impolite:

```{r}
func1 <- function(){
  
  count <- 0
  
  for (i in 1:nrow(d)){
    if (d$polite[i]=="impolite"){
      count <- count + 1
    }
  }
  return(count)
}

system.time(count <- func1())

```

Your solution here:

```{r}
func2 <- function(){
  ...
}
system.time(func2())
```

## 2) Count how many tweets mention Brexit:

```{r}
func1 <- function(){
  brexit_tweets <- c()
  for (i in 1:nrow(d)){
    if (grepl("brexit", d$text[i], ignore.case=TRUE)==TRUE){
      brexit_tweets <- c(brexit_tweets, i)
    }
  }
  return(length(brexit_tweets))
}
system.time(count <- func1())
```

Your solution here:

```{r}
func2 <- function(){
  ...
}
system.time(func2())
```


## 3) Take a random sample of 200 tweets that are classified as "impolite"

```{r}
func1 <- function(){
  
  # sample random uniform number
  d$random <- runif(nrow(d))
  # sort by this random number
  dd <- d[order(d$random),]
  # loop over rows using 'while'
  smp <- data.frame()
  i <- 1
  while (nrow(smp)<200){
    if (d$polite[i]=="impolite"){
      smp <- rbind(smp, d[i,])
    }
    i <- i + 1
  }
  return(smp)

}

system.time(smp <- func1())

```

Your solution here:

```{r}
func2 <- function(){
  ...
}

system.time(func2())
```

