Load Required Libraries

# Install packages if not already installed
if (!require("tidyverse")) install.packages("tidyverse")
if (!require("knitr")) install.packages("knitr")

library(tidyverse)
library(knitr)

Load the Netflix Dataset

# Check if file exists
if (!file.exists("Netflix.csv")) {
  stop("Error: Netflix.csv file not found!")
}

# Read the Netflix dataset
Netflix <- read.csv("Netflix.csv", stringsAsFactors = FALSE)

# Check if data was loaded
if (nrow(Netflix) == 0) {
  stop("Error: Netflix.csv is empty!")
}

Transform Data - Separate Actors

# Separate actors in the cast column and rename the column
Netflix_Actor <- Netflix %>% 
  separate_rows(cast, sep = ", ") %>% 
  drop_na(cast) %>% 
  rename(actor = cast)

# Display the transformed data structure
cat("Transformed dataset dimensions:", dim(Netflix_Actor)[1], "rows and", 
    dim(Netflix_Actor)[2], "columns\n\n")
## Transformed dataset dimensions: 44881 rows and 12 columns
# Display sample of transformed data
head(Netflix_Actor, 10) %>% 
  select(type, title, actor) %>% 
  kable()
type title actor
Movie Norm of the North: King Sized Adventure Alan Marriott
Movie Norm of the North: King Sized Adventure Andrew Toth
Movie Norm of the North: King Sized Adventure Brian Dobson
Movie Norm of the North: King Sized Adventure Cole Howard
Movie Norm of the North: King Sized Adventure Jennifer Cameron
Movie Norm of the North: King Sized Adventure Jonathan Holmes
Movie Norm of the North: King Sized Adventure Lee Tockar
Movie Norm of the North: King Sized Adventure Lisa Durupt
Movie Norm of the North: King Sized Adventure Maya Kay
Movie Norm of the North: King Sized Adventure Michael Dobson

Find Top 6 Actors in TV Shows

# Finding the 6 actors that have the most appearances on TV show
top_tv_actors <- Netflix_Actor %>%
  select(type, actor) %>% 
  filter(type == "TV Show") %>% 
  group_by(actor) %>% 
  count(sort = TRUE) %>% 
  ungroup() %>% 
  head(6)

# Display the results
top_tv_actors %>% 
  kable(col.names = c("Actor", "Number of TV Show Appearances"),
        caption = "Top 6 Actors with Most Appearances in Netflix TV Shows")
Top 6 Actors with Most Appearances in Netflix TV Shows
Actor Number of TV Show Appearances
210
Takahiro Sakurai 18
Yuki Kaji 16
Daisuke Ono 14
David Attenborough 14
Ashleigh Ball 12

Visualize the Results

# Create a bar plot
ggplot(top_tv_actors, aes(x = reorder(actor, n), y = n)) +
  geom_bar(stat = "identity", fill = "#E50914") +
  coord_flip() +
  labs(title = "Top 6 Actors with Most Appearances in Netflix TV Shows",
       x = "Actor",
       y = "Number of Appearances") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5, face = "bold"),
        panel.grid.major.y = element_blank())