<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/using-the-tuber-package-to-analyse-a-youtube-channel/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://insightr.wordpress.com/2017/12/11/using-the-tuber-package-to-analyse-a-youtube-channel/"> R – insightR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p><strong>By Gabriel Vasconcelos</strong></p>
<p>
</div>
<p>So I decided to have a quick look at the <a rel="nofollow" href="https://cran.r-project.org/web/packages/tuber/index.html" target="_blank">tuber</a> package to extract YouTube data in R. My cousin is a singer (a hell of a good one) and he has a YouTube channel (<a rel="nofollow" href="https://www.youtube.com/user/danielvasconceloscp" target="_blank">dan vasc</a>), which I strongly recommend, where he posts his covers. I will focus my analysis on his channel. The tuber package is very friendly and it downloads YouTube statistics on comments, views, likes and more straight to R using the YouTube API.</p>
<p><span></span></p>
<p>First let us look on some general information on the channel (Codes for replication in the end of the text). The table below shows the number of followers, views, videos, etc in the moment I downloaded the data (2017-12-12 11:20pm). If you run the code on your computer the results may be different because the channel will have more activity. Dan’s channel is getting close to 1 million views and he has 58 times more likes than dislikes. His views ratio is 13000 views per video.</p>
<p><span> </span><br>
<font size="1">
<table width="450">
<thead>
<tr>
<th align="left">Channel</th>
<th align="left">Subscriptions</th>
<th align="left">Views</th>
<th align="left">Videos</th>
<th align="right">Likes</th>
<th align="right">Dislikes</th>
<th align="right">Comments</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">Dan Vasc</td>
<td align="left">5127</td>
<td align="left">743322</td>
<td align="left">57</td>
<td align="right">9008</td>
<td align="right">155</td>
<td align="right">1993</td>
</tr>
</tbody>
</table>
<p> </p>
<p>We can also see some of the same statistics for each video. I selected only videos published after January 2016 that is when the channel became more active. The list has 29 videos. You can see that the channel became even more active in 2017. In the last month it started with weekly publications.</p>
<p><font size="1"> 
<table width="450">
<thead>
<tr>
<th align="left">date</th>
<th align="left">title</th>
<th align="right">viewCount</th>
<th align="right">likeCount</th>
<th align="right">dislikeCount</th>
<th align="right">commentCount</th>
</tr>
</thead>
<tbody>
<tr>
<td align="left">2016-03-09</td>
<td align="left">“Heart Of Steel” – MANOWAR cover</td>
<td align="right">95288</td>
<td align="right">1968</td>
<td align="right">53</td>
<td align="right">371</td>
</tr>
<tr>
<td align="left">2016-05-09</td>
<td align="left">“The Sound Of Silence” – SIMON &amp; GARFUNKEL / DISTURBED cover</td>
<td align="right">13959</td>
<td align="right">556</td>
<td align="right">6</td>
<td align="right">85</td>
</tr>
<tr>
<td align="left">2016-07-04</td>
<td align="left">One Man Choir – Handel’s Hallelujah</td>
<td align="right">9390</td>
<td align="right">375</td>
<td align="right">6</td>
<td align="right">70</td>
</tr>
<tr>
<td align="left">2016-08-16</td>
<td align="left">“Carry On” – MANOWAR cover</td>
<td align="right">19146</td>
<td align="right">598</td>
<td align="right">12</td>
<td align="right">98</td>
</tr>
<tr>
<td align="left">2016-09-12</td>
<td align="left">“You Are Loved (Don’t Give Up)” – JOSH GROBAN cover</td>
<td align="right">2524</td>
<td align="right">142</td>
<td align="right">0</td>
<td align="right">21</td>
</tr>
<tr>
<td align="left">2016-09-26</td>
<td align="left">“Hearts On Fire” – HAMMERFALL cover</td>
<td align="right">6584</td>
<td align="right">310</td>
<td align="right">4</td>
<td align="right">58</td>
</tr>
<tr>
<td align="left">2016-10-26</td>
<td align="left">“Dawn Of Victory” – RHAPSODY OF FIRE cover</td>
<td align="right">10335</td>
<td align="right">354</td>
<td align="right">5</td>
<td align="right">69</td>
</tr>
<tr>
<td align="left">2017-04-28</td>
<td align="left">“I Don’t Wanna Miss A Thing” – AEROSMITH cover</td>
<td align="right">9560</td>
<td align="right">396</td>
<td align="right">5</td>
<td align="right">89</td>
</tr>
<tr>
<td align="left">2017-05-09</td>
<td align="left">State of affairs</td>
<td align="right">906</td>
<td align="right">99</td>
<td align="right">1</td>
<td align="right">40</td>
</tr>
<tr>
<td align="left">2017-05-26</td>
<td align="left">“Cha-La Head Cha-La” – DRAGON BALL Z INTRO cover (Japanese)</td>
<td align="right">2862</td>
<td align="right">160</td>
<td align="right">4</td>
<td align="right">39</td>
</tr>
<tr>
<td align="left">2017-05-26</td>
<td align="left">“Cha-La Head Cha-La” – DRAGON BALL Z INTRO cover (Português)</td>
<td align="right">3026</td>
<td align="right">235</td>
<td align="right">3</td>
<td align="right">62</td>
</tr>
<tr>
<td align="left">2017-05-26</td>
<td align="left">“Cha-La Head Cha-La” – DRAGON BALL Z INTRO cover (English)</td>
<td align="right">2682</td>
<td align="right">108</td>
<td align="right">2</td>
<td align="right">14</td>
</tr>
<tr>
<td align="left">2017-06-14</td>
<td align="left">HOW TO BE A YOUTUBE SINGER | ASKDANVASC 01</td>
<td align="right">559</td>
<td align="right">44</td>
<td align="right">1</td>
<td align="right">19</td>
</tr>
<tr>
<td align="left">2017-06-17</td>
<td align="left">Promotional Live || Q&amp;A and video games</td>
<td align="right">206</td>
<td align="right">16</td>
<td align="right">0</td>
<td align="right">2</td>
</tr>
<tr>
<td align="left">2017-07-18</td>
<td align="left">“The Bard’s Song” – BLIND GUARDIAN cover (SPYGLASS INN project)</td>
<td align="right">3368</td>
<td align="right">303</td>
<td align="right">2</td>
<td align="right">47</td>
</tr>
<tr>
<td align="left">2017-07-23</td>
<td align="left">“Numb” – LINKIN PARK cover (R.I.P. CHESTER)</td>
<td align="right">6717</td>
<td align="right">350</td>
<td align="right">14</td>
<td align="right">51</td>
</tr>
<tr>
<td align="left">2017-07-27</td>
<td align="left">THE PERFECT TAKE and HOW TO MIX VOCALS | ASKDANVASC 02</td>
<td align="right">305</td>
<td align="right">29</td>
<td align="right">0</td>
<td align="right">11</td>
</tr>
<tr>
<td align="left">2017-08-04</td>
<td align="left">4000 Subscribers and Second Channel</td>
<td align="right">515</td>
<td align="right">69</td>
<td align="right">1</td>
<td align="right">23</td>
</tr>
<tr>
<td align="left">2017-08-10</td>
<td align="left">“Hello” – ADELE cover [ROCK VERSION]</td>
<td align="right">6518</td>
<td align="right">365</td>
<td align="right">2</td>
<td align="right">120</td>
</tr>
<tr>
<td align="left">2017-08-27</td>
<td align="left">“The Rains Of Castamere” (The Lannister Song) – GAME OF THRONES cover</td>
<td align="right">2174</td>
<td align="right">133</td>
<td align="right">5</td>
<td align="right">28</td>
</tr>
<tr>
<td align="left">2017-08-31</td>
<td align="left">“Africa” – TOTO cover</td>
<td align="right">18251</td>
<td align="right">642</td>
<td align="right">10</td>
<td align="right">172</td>
</tr>
<tr>
<td align="left">2017-09-24</td>
<td align="left">“Chop Suey!” – SYSTEM OF A DOWN cover</td>
<td align="right">2562</td>
<td align="right">236</td>
<td align="right">6</td>
<td align="right">56</td>
</tr>
<tr>
<td align="left">2017-10-09</td>
<td align="left">“An American Trilogy” – ELVIS PRESLEY cover</td>
<td align="right">1348</td>
<td align="right">168</td>
<td align="right">1</td>
<td align="right">48</td>
</tr>
<tr>
<td align="left">2017-11-08</td>
<td align="left">“Beauty And The Beast” – Main Theme cover | Feat. Alina Lesnik</td>
<td align="right">2270</td>
<td align="right">192</td>
<td align="right">2</td>
<td align="right">59</td>
</tr>
<tr>
<td align="left">2017-11-16</td>
<td align="left">“Bohemian Rhapsody” – QUEEN cover</td>
<td align="right">2589</td>
<td align="right">339</td>
<td align="right">3</td>
<td align="right">95</td>
</tr>
<tr>
<td align="left">2017-11-23</td>
<td align="left">“The Phantom Of The Opera” – NIGHTWISH/ANDREW LLOYD WEBBER cover | Feat. Dragica</td>
<td align="right">1857</td>
<td align="right">209</td>
<td align="right">2</td>
<td align="right">42</td>
</tr>
<tr>
<td align="left">2017-11-24</td>
<td align="left">“Back In Black” – AC/DC cover (RIP MALCOLM YOUNG)</td>
<td align="right">2202</td>
<td align="right">207</td>
<td align="right">2</td>
<td align="right">56</td>
</tr>
<tr>
<td align="left">2017-11-30</td>
<td align="left">“Immigrant Song” – LED ZEPPELIN cover</td>
<td align="right">3002</td>
<td align="right">204</td>
<td align="right">1</td>
<td align="right">62</td>
</tr>
<tr>
<td align="left">2017-12-07</td>
<td align="left">“Sweet Child O’ Mine” – GUNS N’ ROSES cover</td>
<td align="right">1317</td>
<td align="right">201</td>
<td align="right">2</td>
<td align="right">86</td>
</tr>
</tbody>
</table>
<p> </p>
<p>Now that we saw the data. Let’s explore it to check for structures and information. The plots below show how likes, dislikes and comments are related to views. The positive relation is obvious. However, we have some degree of nonlinearity in likes and comments. The increment on likes and comments becomes smaller as the views increase. The dislikes look more linear on the views but the number of dislikes is to small to be sure.</p>
<p><img alt="plot of chunk unnamed-chunk-3" src="https://i0.wp.com/i.imgur.com/tRcNXXN.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Another interesting information is how comments are distributed over time in each video. I selected the four most recent videos and plotted the comments time-series below. All videos have a lot of activity in the first days but it decreases fast a few days latter. Followers and subscribers probably see the videos first and they must be responsible for the intense activity in the beginning of each plot.</p>
<p><img alt="plot of chunk unnamed-chunk-4" src="https://i0.wp.com/i.imgur.com/y7eAQmC.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The most important information might be how the channel grows over the time. Dan’s channel had two important moments in 2017. It became much more active in April and it started having weekly publications in November. We can clearly see that both strategies worked in the plot below. I put two dashed lines to show these two events. In April the number of comments increased a lot and they increased even more in November.</p>
<p><img alt="plot of chunk unnamed-chunk-5" src="https://i2.wp.com/i.imgur.com/AFp7eTJ.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Finally, let’s have a look at what is in the comments using a WordCloud (<a rel="nofollow" href="https://cran.r-project.org/web/packages/wordcloud/index.html" target="_blank">wordcloud package</a>). I removed words that are not informative such as “you, was, is, were” for English and Portuguese. The result is just below.</p>
<p><img alt="plot of chunk unnamed-chunk-6" src="https://i1.wp.com/i.imgur.com/XpKsv7r.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>Codes</h2>
<p>Before using the tuber package you need an ID and a password from Google Developer Console. Click <a rel="nofollow" href="https://cran.r-project.org/web/packages/tuber/vignettes/tuber-ex.html" target="_blank">here</a> for more information. If you are interested, the package <a rel="nofollow" href="https://cran.r-project.org/web/packages/tubern/index.html" target="_blank">tubern</a> has some other tools to work with YouTube data such as generating reports.</p>
<pre>library(tuber)
library(tidyverse)
library(lubridate)
library(stringi)
library(wordcloud)
library(gridExtra)

httr::set_config( config( ssl_verifypeer = 0L ) ) # = Fixes some certificate problems on linux = #

# = Autentication = #
yt_oauth(&quot;ID&quot;,
         &quot;PASS&quot;,token = &quot;&quot;)

# = Download and prepare data = #

# = Channel stats = #
chstat = get_channel_stats(&quot;UCbZRdTukTCjFan4onn04sDA&quot;)

# = Videos = #
videos = yt_search(term=&quot;&quot;, type=&quot;video&quot;, channel_id = &quot;UCbZRdTukTCjFan4onn04sDA&quot;)
videos = videos %>%
  mutate(date = as.Date(publishedAt)) %>%
  filter(date > &quot;2016-01-01&quot;) %>%
  arrange(date)

# = Comments = #
comments = lapply(as.character(videos$video_id), function(x){
  get_comment_threads(c(video_id = x), max_results = 1000)
})

# = Prep the data = #
# = Video Stat Table = #
videostats = lapply(as.character(videos$video_id), function(x){
  get_stats(video_id = x)
})
videostats = do.call(rbind.data.frame, videostats)
videostats$title = videos$title
videostats$date = videos$date
videostats = select(videostats, date, title, viewCount, likeCount, dislikeCount, commentCount) %>%
  as.tibble() %>%
  mutate(viewCount = as.numeric(as.character(viewCount)),
         likeCount = as.numeric(as.character(likeCount)),
         dislikeCount = as.numeric(as.character(dislikeCount)),
         commentCount = as.numeric(as.character(commentCount)))

# = General Stat Table = #
genstat = data.frame(Channel=&quot;Dan Vasc&quot;, Subcriptions=chstat$statistics$subscriberCount,
                   Views = chstat$statistics$viewCount,
                   Videos = chstat$statistics$videoCount, Likes = sum(videostats$likeCount),
                   Dislikes = sum(videostats$dislikeCount), Comments = sum(videostats$commentCount))

# = videostats Plot = #
p1 = ggplot(data = videostats[-1, ]) + geom_point(aes(x = viewCount, y = likeCount))
p2 = ggplot(data = videostats[-1, ]) + geom_point(aes(x = viewCount, y = dislikeCount))
p3 = ggplot(data = videostats[-1, ]) + geom_point(aes(x = viewCount, y = commentCount))
grid.arrange(p1, p2, p3, ncol = 2)

# = Comments TS = #
comments_ts = lapply(comments, function(x){
  as.Date(x$publishedAt)
})
comments_ts = tibble(date = as.Date(Reduce(c, comments_ts))) %>%
  group_by(date) %>% count()
ggplot(data = comments_ts) + geom_line(aes(x = date, y = n)) +
  geom_smooth(aes(x = date, y = n), se = FALSE) + ggtitle(&quot;Comments by day&quot;)+
  geom_vline(xintercept = as.numeric(as.Date(&quot;2017-11-08&quot;)), linetype = 2,color = &quot;red&quot;)+
  geom_vline(xintercept = as.numeric(as.Date(&quot;2017-04-28&quot;)), linetype = 2,color = &quot;red&quot;)

# = coments by video = #
selected = (nrow(videostats) - 3):nrow(videostats)
top4 = videostats$title[selected]
top4comments = comments[selected]

p = list()
for(i in 1:4){
  df = top4comments[[i]]
  df$date = as.Date(df$publishedAt)
  df = df %>%
    arrange(date) %>%
    group_by(year(date), month(date), day(date)) %>%
    count()
  df$date = make_date(df$`year(date)`, df$`month(date)`,df$`day(date)`)
  p[[i]] = ggplot(data=df) + geom_line(aes(x = date, y = n)) + ggtitle(top4[i])
}
do.call(grid.arrange,p)

## = WordClouds = ##
comments_text = lapply(comments,function(x){
  as.character(x$textOriginal)
})
comments_text = tibble(text = Reduce(c, comments_text)) %>%
  mutate(text = stri_trans_general(tolower(text), &quot;Latin-ASCII&quot;))
remove = c(&quot;you&quot;,&quot;the&quot;,&quot;que&quot;,&quot;and&quot;,&quot;your&quot;,&quot;muito&quot;,&quot;this&quot;,&quot;that&quot;,&quot;are&quot;,&quot;for&quot;,&quot;cara&quot;,
         &quot;from&quot;,&quot;very&quot;,&quot;like&quot;,&quot;have&quot;,&quot;voce&quot;,&quot;man&quot;,&quot;one&quot;,&quot;nao&quot;,&quot;com&quot;,&quot;with&quot;,&quot;mais&quot;,
         &quot;was&quot;,&quot;can&quot;,&quot;uma&quot;,&quot;but&quot;,&quot;ficou&quot;,&quot;meu&quot;,&quot;really&quot;,&quot;seu&quot;,&quot;would&quot;,&quot;sua&quot;,&quot;more&quot;,
         &quot;it's&quot;,&quot;it&quot;,&quot;is&quot;,&quot;all&quot;,&quot;i'm&quot;,&quot;mas&quot;,&quot;como&quot;,&quot;just&quot;,&quot;make&quot;,&quot;what&quot;,&quot;esse&quot;,&quot;how&quot;,
         &quot;por&quot;,&quot;favor&quot;,&quot;sempre&quot;,&quot;time&quot;,&quot;esta&quot;,&quot;every&quot;,&quot;para&quot;,&quot;i've&quot;,&quot;tem&quot;,&quot;will&quot;,
         &quot;you're&quot;,&quot;essa&quot;,&quot;not&quot;,&quot;faz&quot;,&quot;pelo&quot;,&quot;than&quot;,&quot;about&quot;,&quot;acho&quot;,&quot;isso&quot;,
         &quot;way&quot;,&quot;also&quot;,&quot;aqui&quot;,&quot;been&quot;,&quot;out&quot;,&quot;say&quot;,&quot;should&quot;,&quot;when&quot;,&quot;did&quot;,&quot;mesmo&quot;,
         &quot;minha&quot;,&quot;next&quot;,&quot;cha&quot;,&quot;pra&quot;,&quot;sei&quot;,&quot;sure&quot;,&quot;too&quot;,&quot;das&quot;,&quot;fazer&quot;,&quot;made&quot;,
         &quot;quando&quot;,&quot;ver&quot;,&quot;cada&quot;,&quot;here&quot;,&quot;need&quot;,&quot;ter&quot;,&quot;don't&quot;,&quot;este&quot;,&quot;has&quot;,&quot;tambem&quot;,
         &quot;una&quot;,&quot;want&quot;,&quot;ate&quot;,&quot;can't&quot;,&quot;could&quot;,&quot;dia&quot;,&quot;fiquei&quot;,&quot;num&quot;,&quot;seus&quot;,&quot;tinha&quot;,&quot;vez&quot;,
         &quot;ainda&quot;,&quot;any&quot;,&quot;dos&quot;,&quot;even&quot;,&quot;get&quot;,&quot;must&quot;,&quot;other&quot;,&quot;sem&quot;,&quot;vai&quot;,&quot;agora&quot;,&quot;desde&quot;,
         &quot;dessa&quot;,&quot;fez&quot;,&quot;many&quot;,&quot;most&quot;,&quot;tao&quot;,&quot;then&quot;,&quot;tudo&quot;,&quot;vou&quot;,&quot;ficaria&quot;,&quot;foi&quot;,&quot;pela&quot;,
         &quot;see&quot;,&quot;teu&quot;,&quot;those&quot;,&quot;were&quot;)
words = tibble(word = Reduce(c, stri_extract_all_words(comments_text$text))) %>%
  group_by(word) %>% count() %>% arrange(desc(n)) %>% filter(nchar(word) >= 3) %>%
  filter(n > 10 &amp; word %in% remove == FALSE) 

set.seed(3)
wordcloud(words$word, words$n, random.order = FALSE, random.color = TRUE,
          rot.per = 0.3, colors = 1:nrow(words))
</pre>
<p> <a rel="nofollow" href="https://feeds.wordpress.com/1.0/gocomments/insightr.wordpress.com/787/" target="_blank"><img border="0" alt="" src="https://feeds.wordpress.com/1.0/comments/insightr.wordpress.com/787/"></a> <img border="0" alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" data-recalc-dims="1"></p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://insightr.wordpress.com/2017/12/11/using-the-tuber-package-to-analyse-a-youtube-channel/"> R – insightR</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/jNWvUSHtsbQ" height="1"></a></div></font></font>