This assignment centers around the youtubers.csv
dataset, providing insights into the top 1000 YouTube influencers as of
Sep 2022. It encompasses key metrics like ranking, subscribers, views,
and engagement. Additionally, a supplementary dataset
channel.csv complements this information with data on
video uploads and the year of channel creation. The tasks ahead involve
data wrangling, exploratory analysis, classification, and comparative
analysis of influencers, offering a comprehensive understanding of their
impact and reach.
# Load the dataset
youtubers <- read.csv("youtubers.csv")
# Display the first few rows of the dataset
head(youtubers)
## S.no channel Youtuber country category
## 1 1 T-Series tseries India Music & Dance
## 2 2 Cocomelon - Nursery Rhymes checkgate <NA> Education
## 3 3 PewDiePie PewDiePie United States Animation
## 4 4 MrBeast MrBeast6000 United States Video games
## 5 5 ✿ Kids Diana Show ✿ Kids Diana Show <NA> Animation
## 6 6 Like Nastya Like Nastya <NA> Toys
## Category_3 subscribers X.views..Avg.... X.likes..Avg.... X.comments..Avg....
## 1 224400000 141500 4600 125
## 2 142500000 12500000 64800 0
## 3 111600000 1900000 130500 7400
## 4 Humor 104100000 44800000 2200000 70900
## 5 Toys 100500000 6900000 34800 0
## 6 100400000 5300000 28100 0
# Get summary statistics
summary(youtubers)
## S.no channel Youtuber country
## Min. : 1.0 Length:1047 Length:1047 Length:1047
## 1st Qu.: 262.5 Class :character Class :character Class :character
## Median : 524.0 Mode :character Mode :character Mode :character
## Mean : 521.8
## 3rd Qu.: 785.5
## Max. :1000.0
## category Category_3 subscribers X.views..Avg....
## Length:1047 Length:1047 Min. : 9900000 Min. : 0
## Class :character Class :character 1st Qu.: 11400000 1st Qu.: 46000
## Mode :character Mode :character Median : 14000000 Median : 225500
## Mean : 18366476 Mean : 1237201
## 3rd Qu.: 19700000 3rd Qu.: 881250
## Max. :224400000 Max. :51800000
## X.likes..Avg.... X.comments..Avg....
## Min. : 0 Min. : 0.0
## 1st Qu.: 838 1st Qu.: 5.0
## Median : 4500 Median : 121.0
## Mean : 55038 Mean : 2305.6
## 3rd Qu.: 31850 3rd Qu.: 915.5
## Max. :4400000 Max. :186800.0
# Display the structure of the dataset
str(youtubers)
## 'data.frame': 1047 obs. of 10 variables:
## $ S.no : int 1 2 3 4 5 6 7 8 9 10 ...
## $ channel : chr "T-Series" "Cocomelon - Nursery Rhymes" "PewDiePie" "MrBeast" ...
## $ Youtuber : chr "tseries" "checkgate" "PewDiePie" "MrBeast6000" ...
## $ country : chr "India" NA "United States" "United States" ...
## $ category : chr "Music & Dance" "Education" "Animation" "Video games" ...
## $ Category_3 : chr "" "" "" "Humor" ...
## $ subscribers : num 2.24e+08 1.42e+08 1.12e+08 1.04e+08 1.00e+08 ...
## $ X.views..Avg.... : num 141500 12500000 1900000 44800000 6900000 ...
## $ X.likes..Avg.... : int 4600 64800 130500 2200000 34800 28100 4800 1400 14400 1400000 ...
## $ X.comments..Avg....: int 125 0 7400 70900 0 0 205 54 0 42000 ...
# Loading dplyr package
library(dplyr)
# Rename the specified columns
youtubers <- youtubers %>%
rename(
ranking = S.no,
views = X.views..Avg....,
likes = X.likes..Avg....,
comments = X.comments..Avg....
)
# Remove the column "Category_3"
youtubers <- youtubers %>%
select(-Category_3)
# Display the first few rows with updates
head(youtubers)
## ranking channel Youtuber country
## 1 1 T-Series tseries India
## 2 2 Cocomelon - Nursery Rhymes checkgate <NA>
## 3 3 PewDiePie PewDiePie United States
## 4 4 MrBeast MrBeast6000 United States
## 5 5 ✿ Kids Diana Show ✿ Kids Diana Show <NA>
## 6 6 Like Nastya Like Nastya <NA>
## category subscribers views likes comments
## 1 Music & Dance 224400000 141500 4600 125
## 2 Education 142500000 12500000 64800 0
## 3 Animation 111600000 1900000 130500 7400
## 4 Video games 104100000 44800000 2200000 70900
## 5 Animation 100500000 6900000 34800 0
## 6 Toys 100400000 5300000 28100 0
# Check if there are 1000 unique YouTubers
if (nrow(youtubers) == 1000) {
unique_youtubers <- youtubers # No need to remove duplicates, as there are already 1000 unique YouTubers
} else {
# Remove duplicate rows based on "ranking" column
unique_youtubers <- youtubers %>%
distinct(ranking, .keep_all = TRUE)
print("Duplicate rows based on 'ranking' column have been removed.")
}
## [1] "Duplicate rows based on 'ranking' column have been removed."
# Save the result in a new dataframe
new_youtubers <- unique_youtubers
# Convert 'country' and 'category' to factor variables
new_youtubers <- new_youtubers %>%
mutate(country = as.factor(country),
category = as.factor(category))
# Summary of 'country' and 'category'
summary(new_youtubers$country)
## Algeria Argentina Bangladesh Brazil Colombia
## 5 6 3 79 10
## Ecuador Egypt France India Indonesia
## 1 7 5 208 38
## Iraq Japan Jordan Malaysia Mexico
## 6 4 1 1 66
## Morocco Pakistan Peru Philippines Russia
## 3 7 2 13 32
## Saudi Arabia Somalia Spain Thailand Turkey
## 4 1 5 15 4
## United Kingdom United States NA's
## 9 293 172
summary(new_youtubers$category)
## Animals & Pets Animation ASMR
## 2 94 2
## Autos & Vehicles Beauty Daily vlogs
## 2 2 37
## Design/art DIY & Life Hacks Education
## 3 10 32
## Fashion Fitness Food & Drinks
## 3 3 16
## Health & Self Help Humor Movies
## 3 15 101
## Music & Dance Mystery News & Politics
## 226 1 49
## Science & Technology Sports Toys
## 17 9 21
## Travel Video games NA's
## 1 63 288
# Are there NA values? Let's check
na_counts <- colSums(is.na(new_youtubers))
# Print the number of NA values
print(na_counts)
## ranking channel Youtuber country category subscribers
## 0 0 0 172 288 0
## views likes comments
## 0 0 0
# Count unique countries and categories (omitting NA values)
unique_countries <- new_youtubers %>%
filter(!is.na(country)) %>%
summarize(unique_countries = n_distinct(country))
unique_categories <- new_youtubers %>%
filter(!is.na(category)) %>%
summarize(unique_categories = n_distinct(category))
# Print the results
print(unique_countries)
## unique_countries
## 1 27
print(unique_categories)
## unique_categories
## 1 23
# Count the number of YouTubers per country and category (omitting NA values)
top_countries <- new_youtubers %>%
filter(!is.na(country)) %>%
count(country, sort = TRUE) %>%
slice_head(n = 3)
top_categories <- new_youtubers %>%
filter(!is.na(category)) %>%
count(category, sort = TRUE) %>%
slice_head(n = 3)
# Print the results
print(top_countries)
## country n
## 1 United States 293
## 2 India 208
## 3 Brazil 79
print(top_categories)
## category n
## 1 Music & Dance 226
## 2 Movies 101
## 3 Animation 94
# Create a new column 'type' based on subscriber count
new_youtubers <- new_youtubers %>%
mutate(type = case_when(
subscribers > 100000000 ~ "super influencer",
subscribers >= 10000000 & subscribers <= 100000000 ~ "mega influencer",
subscribers >= 1000000 & subscribers < 10000000 ~ "power influencer",
TRUE ~ "other"
))
# Group by 'type' and count the number of YouTubers in each category
classification_count <- new_youtubers %>%
group_by(type) %>%
summarize(count = n())
# Print the result
print(classification_count)
## # A tibble: 3 × 2
## type count
## <chr> <int>
## 1 mega influencer 977
## 2 power influencer 17
## 3 super influencer 6
# Apply log transformation to 'views'
new_youtubers$log_views <- log(new_youtubers$views + 1)
# Create a boxplot for the distribution of views by influencer type
new_youtubers %>%
ggplot(aes(x = type, y = log_views, fill = type)) +
geom_boxplot(show.legend = FALSE) +
labs(title = "Distribution of Views by Influencer Type (Log Scale)",
x = "Influencer Type",
y = "Views") +
theme_bw()+
theme(axis.text.x = element_text(vjust = 0.5, hjust = 1),
plot.title = element_text(hjust = 0.5))
# Importing channel.csv
channel_data <- read.csv("channel.csv")
# Get a basic idea of the channel data
head(channel_data)
## video year channel_id
## 1 580 2007 Harsh Beniwal
## 2 10000 2013 Like Nastya Vlog
## 3 500 2013 TheWillyrex
## 4 754 2010 YRF
## 5 2474 2012 RobleisIUTU
## 6 500 2008 Mythpat
merged_data <- merge(new_youtubers, channel_data, by.x = "channel", by.y = "channel_id", all.x = TRUE)
# Get a basic idea of the merged dataset
head(merged_data)
## channel ranking Youtuber country category
## 1 [í–„ì§€]Hamzy 937 [í–„ì§€]Hamzy United States <NA>
## 2 #Refugio Mental 520 #Refugio Mental Argentina Movies
## 3 #Refúgio Mental 629 #Refúgio Mental Brazil Movies
## 4 ★ Kids Roma Show 267 ★ Kids Roma Show <NA> Animation
## 5 ✿ Kids Diana Show 5 ✿ Kids Diana Show <NA> Animation
## 6 123 GO! 728 123 GO! United States DIY & Life Hacks
## subscribers views likes comments type log_views video year
## 1 10200000 1900000 40900 1000 mega influencer 14.45736 10000 2009
## 2 14100000 566800 6600 80 mega influencer 13.24776 3206 2015
## 3 12800000 341700 9800 89 mega influencer 12.74169 561 2014
## 4 19500000 1300000 8300 0 mega influencer 14.07788 692 2015
## 5 100500000 6900000 34800 0 super influencer 15.74703 1204 2009
## 6 11800000 374600 2900 99 mega influencer 12.83362 4304 2012
# Group by 'type' and calculate the average number of videos uploaded
average_videos <- merged_data %>%
group_by(type) %>%
summarise(avg_videos = round(mean(video, na.rm = TRUE)))
# Print the result
print(average_videos)
## # A tibble: 3 × 2
## type avg_videos
## <chr> <dbl>
## 1 mega influencer 2356
## 2 power influencer 1525
## 3 super influencer 2696
The average number of videos uploaded to YouTube for each of the
three influencer types are:
Super influencer -
2696
Mega influencer - 2356
Power
influencer - 1525
# Filter data for mega influencers
mega_influencers <- merged_data %>%
filter(type == "mega influencer")
# Apply logarithmic transformation to both 'videos' and 'comments'
mega_influencers$log_videos <- log(mega_influencers$video + 1)
mega_influencers$log_comments <- log(mega_influencers$comments + 1)
# Create a scatter plot with a linear fit line using the transformed variables
ggplot(mega_influencers, aes(x = log_videos, y = log_comments)) +
geom_point(alpha = 0.7, color = "#FF6F61") +
geom_smooth(method = "lm", color = "blue", se = FALSE) + # Add linear fit line
labs(title = "Relationship between Log(Videos) and Log(Comments) for Mega Influencers",
x = "Number of Videos", y = "Number of Comments") +
theme_minimal()
# Apply square root transformation to both 'videos' and 'comments'
mega_influencers$sqrt_videos <- sqrt(mega_influencers$video)
mega_influencers$sqrt_comments <- sqrt(mega_influencers$comments)
# Create a scatter plot with a linear fit line using the transformed variables
ggplot(mega_influencers, aes(x = sqrt_videos, y = sqrt_comments)) +
geom_point(alpha = 0.7, color = "#FF6F61") +
geom_smooth(method = "lm", color = "blue", se = FALSE) + # Add linear fit line
labs(title = "Relationship between Sqrt(Videos) and Sqrt(Comments) for Mega Influencers",
x = "Number of Videos", y = "Number of Comments") +
theme_minimal()
# Filter data for channels in the United States and India (excluding other countries and NA values)
filtered_data <- merged_data %>%
filter(country %in% c("United States", "India"))
# Calculate the means of views, likes, and comments for both countries
summary_stats <- filtered_data %>%
group_by(country) %>%
summarise(
mean_views = round(mean(views, na.rm = TRUE)),
mean_likes = round(mean(likes, na.rm = TRUE)),
mean_comments = round(mean(comments, na.rm = TRUE))
)
# Sort the summarized data by mean views in descending order
summary_stats <- summary_stats[order(-summary_stats$mean_views), ]
# Print the sorted summary statistics
print(summary_stats)
## # A tibble: 2 × 4
## country mean_views mean_likes mean_comments
## <fct> <dbl> <dbl> <dbl>
## 1 United States 1485326 60694 2558
## 2 India 1316478 86862 2650
# Assuming 'filtered_data' is your dataframe
ggplot(data = filtered_data, aes(x = log(video), fill = country)) +
geom_histogram(binwidth = 0.2, alpha = 0.5, position = "identity", color = "black") +
geom_density(alpha = 0.2, color = "black") +
labs(title = "Distribution of Videos Uploaded to YouTube",
x = "Log(Number of Videos)", y = "Density") +
scale_fill_manual(values = c("dodgerblue", "firebrick3")) + # Adjusted colors
theme_minimal() +
theme(legend.position = "top")
ggplot(data = filtered_data, aes(x = sqrt(video), fill = country)) +
geom_histogram(binwidth = 2, alpha = 0.5, position = "identity", color = "black") +
geom_density(alpha = 0.2, color = "black") +
labs(title = "Distribution of Videos Uploaded to YouTube",
x = "Square Root of Number of Videos", y = "Density") +
scale_fill_manual(values = c("dodgerblue", "firebrick3")) +
theme_minimal() +
theme(legend.position = "top")
filtered_data %>%
group_by(year, country) %>%
summarise(count = n()) %>%
ggplot(aes(x = year, y = count, color = country, group = country)) +
geom_line() +
labs(title = "Count of YouTube Channels Created Each Year",
x = "Year of Creation", y = "Channel Count") +
scale_color_manual(values = c("blue", "red")) +
theme_minimal()