First things first: load the libraries and the data.

```{r}
library(tidyverse)                                    # Activates the tidyverse
setwd("~/Documents/IT/COURS/2021/R4DS/Data/Movies")   # Changes the working directory
movies <- read_csv("movie_metadata.csv")              # Loads the data
movies <- movies %>% na.omit()                        # Removes instances with missing points
movies <- data.frame(movies)                          # (technical move)
movies <- as_tibble(movies)                           # Create a neat tibble
```

So far: we have imported the data and removed missing values.

Small problem, there are some **duplicates**! This trick below removes them by looking at duplicated movie titles.

```{r}
movies <- movies[!duplicated(movies$movie_title),]    # Remove duplicate titles
```

Now we can move forward with some simple formatting. 

```{r}
movies <- movies %>%                                # Start from the current variable
    mutate(title = movie_title,                     # Rename title (add a new column)
           director = director_name,                # Rename director field (add a ne column) ...
           actor_1 = actor_1_name,
           actor_2 = actor_2_name,
           actor_3 = actor_3_name,
           year = title_year,
           rating = content_rating,
           nb_voters = num_voted_users,
           likes = cast_total_facebook_likes,
           budget = round(budget / 10^3),           # Round the number of K$ of budget
           earnings = round(gross / 10^3)           # Round the number of K$ of profit
           ) %>%
        filter(budget > 1,                          # Keep films with big enough budget..
               earnings > 1) %>%                    # ... and big enough earnings!
    select(title, director, actor_1, actor_2, actor_3, year, color, duration, language, country, rating, 
           nb_voters, likes, budget, earnings, imdb_score)
```

Finally, we save the document.

```{r}
save(movies, file = "movies.RData")
```

