{"id":4391,"date":"2019-01-02T17:06:46","date_gmt":"2019-01-02T15:06:46","guid":{"rendered":"https:\/\/engel-wolf.com\/?p=4391"},"modified":"2019-10-27T17:10:43","modified_gmt":"2019-10-27T15:10:43","slug":"music-listener-statistics-last-fms-last-year-as-an-r-package","status":"publish","type":"post","link":"https:\/\/engel-wolf.com\/?p=4391","title":{"rendered":"Music listener statistics: last.fm\u2019s last.year as an R package"},"content":{"rendered":"\n<p>When starting analyzing last.fm scrobbles with the last.week and last.year function I was always missing some plots or a pure data table. That is why I developed the package \u201c<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/zappingseb\/analyze_last_fm\" target=\"_blank\">analyzelastfm<\/a>\u201d as a simple R6 implementation. I wanted to have different album statistics, as e.g the #of plays per album, divided by the # of tracks on the album. This is now implemented. To get music listening statistic you would start with:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: r; title: ; notranslate\" title=\"\">\n# If you don&#039;t have it, install devtools\n# install.packages(&quot;devtools&quot;)\ndevtools::install_github(&quot;zappingseb\/analyze_last_fm&quot;)\nlibrary(analyzelastfm)\n<\/pre><\/div>\n\n\n<p>First it allows you to import your\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/www.last.fm\/user\/zappingseb\/listening-report\/year\" target=\"_blank\">last.year<\/a>\u00a0data by using the last.fm REST API. Therefore you need to have a last.fm API key. This can be derived by simply going to the\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/www.last.fm\/api\/account\/create\" target=\"_blank\">last.fm API website<\/a>. From there you will get the 32 character long key. After receiving the key I got my last.fm data by:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\napi_key &lt;- &quot;PLEASE ENTER YOUR KEY HERE&quot;\ndata &lt;- UserData$new(&quot;zappingseb&quot;,api_key,2018)\n<\/pre><\/div>\n\n\n<p>The data object now contains the function\u00a0<code>albumstats<\/code>\u00a0that allows me to get the specific information I wanted. I\u2019ll exclude the artist \u201cDie drei\u00a0???\u201d as it\u2019s a kids detective story from Germany I listen to a lot and which\u2019s albums are split into 1 minute tracks, that really screws up my statistic.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: r; title: ; notranslate\" title=\"\">\nView(data$albumstats(\n  sort_by=&quot;by_album_count&quot;, # album track plays \/ nr tracks on album\n  exclude_artist=&quot;Die drei ???&quot;, # exclude my audio book favorite\n  exclude_album=c(&quot;&quot;), # exclude tracks without album name\n  min_tracks=5) # have minimum 5 tracks on the album (NO EPs)\n)\n<\/pre><\/div>\n\n\n<p>The result looks like that:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*hdD4ETXB47W1S8lCyMTPNA.png\" alt=\"\"\/><figcaption>Album statistics of 2018 of zappingseb<\/figcaption><\/figure>\n\n\n\n<p>The statistic shows&nbsp;<code>n<\/code>&nbsp;the number of plays,&nbsp;<code>count<\/code>&nbsp;the number of tracks on the album and&nbsp;<code>count_by_track=n\/count<\/code>&nbsp;.<\/p>\n\n\n\n<p>The top 5 albums you can find here:<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/www.amazon.de\/gp\/product\/B0762Y3T14\/ref=as_li_tl?ie=UTF8&amp;tag=zappingseb-21&amp;camp=1638&amp;creative=6742&amp;linkCode=as2&amp;creativeASIN=B0762Y3T14&amp;linkId=c2e12707d54eee718e3b30b91a967fed\" rel=\"noreferrer noopener\" target=\"_blank\">Hackney Collery Band\u200a\u2014\u200aSharpener<\/a><\/li><li><a href=\"https:\/\/www.amazon.de\/gp\/product\/B06XS8TTS4\/ref=as_li_tl?ie=UTF8&amp;tag=zappingseb-21&amp;camp=1638&amp;creative=6742&amp;linkCode=as2&amp;creativeASIN=B06XS8TTS4&amp;linkId=f712ea96a435a93e069b778990d66645\" rel=\"noreferrer noopener\" target=\"_blank\">Mr Jukes\u200a\u2014\u200aGod first<\/a><\/li><li><a href=\"https:\/\/www.amazon.de\/gp\/product\/B01M24YCCH\/ref=as_li_tl?ie=UTF8&amp;tag=zappingseb-21&amp;camp=1638&amp;creative=6742&amp;linkCode=as2&amp;creativeASIN=B01M24YCCH&amp;linkId=8f3115b23d7953db404d1710a6cb797d\" rel=\"noreferrer noopener\" target=\"_blank\">Electro Deluxe\u200a\u2014\u200aCircle<\/a><\/li><li><a href=\"https:\/\/www.amazon.de\/gp\/product\/B07BS3SFDC\/ref=as_li_tl?ie=UTF8&amp;tag=zappingseb-21&amp;camp=1638&amp;creative=6742&amp;linkCode=as2&amp;creativeASIN=B07BS3SFDC&amp;linkId=32d00c3201b0dc902a9c830a459ed266\" rel=\"noreferrer noopener\" target=\"_blank\">Tom Misch\u200a\u2014\u200aGeography<\/a><\/li><li><a href=\"https:\/\/www.amazon.de\/gp\/product\/B01M0G01UU\/ref=as_li_tl?ie=UTF8&amp;camp=1638&amp;creative=6742&amp;creativeASIN=B01M0G01UU&amp;linkCode=as2&amp;tag=zappingseb-21&amp;linkId=abf7103142c4e6c2895766cd1c2d3dcc\" rel=\"noreferrer noopener\" target=\"_blank\">Bilderbuch\u200a\u2014\u200aMagic Life<\/a><\/li><\/ul>\n\n\n\n<p>Additionally to such calculations, I was also interested in when I was listening to music. Therefore I added some plots to the package. The first one is the listening clock:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data$clock.plot()<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*r3l0qQzJraIley_HmETh6A.png\" alt=\"\"\/><figcaption>Clock plot for my music&nbsp;history<\/figcaption><\/figure>\n\n\n\n<p>I listen to music mostly on my way to work on the bike between 7 and 8 am and between 5 and 6 pm. My most important statistic though is which time of the year I spent listening to music. I\u2019m most interested in some specific days and an average play\/month, that can tell me in which mood I was in. Therefore I created the function\u00a0<code>daily_month_plot<\/code>\u00a0. It plots the average plays per month and the daily plays in one plot. The average plays per month are visualized behind the daily spikes. Here you can see that February was a really quiet month for me.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data$daily_month_plot()<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*CUzCRzku2c8jQpyPj82oGQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>For some simple statistics, I also included an aggregation plotting function called&nbsp;<code>barplots<\/code>&nbsp;. It can plot aggregations per weekday, week, month or day. The data for this function is provided by the function&nbsp;<code>bar_data<\/code>&nbsp;.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data$barplots(\"weekdays\")<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*xrh_HHV7NDqpbsnkeSjO5g.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>weekly (starting with week 52 of 2017):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">data$barplots(\u201cweek\u201d)<\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*NONn2_xzHZ_NRc8SARpEHw.png\" alt=\"\"\/><\/figure>\n\n\n\n<p>Where I can clearly see that I was not really listening to a lot of music during the beginning of the year and my listening activity increased dramatically by week 18.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>This post is a bit out of my spectrum, which mostly deals with packages about&nbsp;<a href=\"https:\/\/towardsdatascience.com\/interesting-packages-taken-from-r-pharma-e89639788180\" rel=\"noreferrer noopener\" target=\"_blank\">R packages being useful for Pharma<\/a>. But I really like music and think music and data science make a good combination. Sound\u2019s great together!<\/p>\n\n\n\n<p>Please find the R package at:&nbsp;<a href=\"https:\/\/github.com\/zappingseb\/analyze_last_fm\" rel=\"noreferrer noopener\" target=\"_blank\">https:\/\/github.com\/zappingseb\/analyze_last_fm<\/a>\u200a\u2014\u200anever too late to star it&nbsp;\ud83d\ude09<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When starting analyzing last.fm scrobbles with the last.week and last.year function I was always missing some plots or a pure data table. That is why I developed the package \u201canalyzelastfm\u201d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4392,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1],"tags":[409,384,411,381,388],"_links":{"self":[{"href":"https:\/\/engel-wolf.com\/index.php?rest_route=\/wp\/v2\/posts\/4391"}],"collection":[{"href":"https:\/\/engel-wolf.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/engel-wolf.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/engel-wolf.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/engel-wolf.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4391"}],"version-history":[{"count":1,"href":"https:\/\/engel-wolf.com\/index.php?rest_route=\/wp\/v2\/posts\/4391\/revisions"}],"predecessor-version":[{"id":4393,"href":"https:\/\/engel-wolf.com\/index.php?rest_route=\/wp\/v2\/posts\/4391\/revisions\/4393"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/engel-wolf.com\/index.php?rest_route=\/wp\/v2\/media\/4392"}],"wp:attachment":[{"href":"https:\/\/engel-wolf.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/engel-wolf.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/engel-wolf.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}