, , , , , ,

Cluster Analysis on Data from Last.FM : A Tutorial for Beginners

Before I begin, I understand that the visitor to this tutorial already has basic understanding in Cluster Analysis (Please excuse me for that).

First, you have to download and install package “RLastFM” from the following location:

Download RLastFM on your computer and install it using R. Since this package is no more supported on CRAN, it is archived.

After installing, please go through the website http://last.fm and register as user. After completing the registration process, login to that website and go to http://www.last.fm/api webpage. Locate “Create an API account” link (on the right, top side of the page):


Click on that link and create an API account. After successful creation of account go to webpage http://www.last.fm/api/accounts and locate the API Key and Secret Key. Copy API Key.

On R console type the following:

# setting global parameters

> lastkey <- "your API key"   # paste the API key what you just have copied from Last.FM website

# Suppose, you want to call info upon “search for an artist” through last.fm API, at least once you have to run the following:

> artist.search(searchterm, limit = NA, page = NA, key = lastkey, parse = TRUE)

here, arguments in the above command are:

Search term, in quotes e.g. “Madonna” for searching artist named Madonna
Limit of results to return. Defaults to maximum value of 20.
Results are paginated. If more than the first 20 results are requested, start at page = 2
API key, defaults to global variable lastkey
Parse the returned XML into a list? Defaults to TRUE.
And this command, when run, returns the following Values:

If parse = FALSE, a object of type “XMLInternalDocument” and “XMLAbstractDocument”

If parse = TRUE, then

Artist Name
Artist mbid (unique identifier for that artist)

# now, search for artist Madonna and store the search results in data frame

> test1 = artist.search("Madonna", key = lastkey, parse = TRUE)

> test2 = data.frame(data1)

> test2

This returns the following data table:


And all the artists in the database which has “Madonna” as artist’s name.

For all the syntax and commands, in working with LastFM APIs, please visit:


Now suppose you want to search for the “album”, here it is how (syntax):

> album.search(searchterm, key = lastkey, limit = NA, parse = TRUE)

This returns the Values:

If parse = FALSE, a object of type “XMLInternalDocument” and “XMLAbstractDocument”

If parse = TRUE, then

Name of matching albums e.g. “Californication”
Name of matching artists e.g. “Red Hot Chilli Peppers”
> test2 = album.search("Californication", key = lastkey, parse = TRUE)
> test3 = data.frame(test2)
> test3

Returns the following result:


Finally, we will learn one more command out of many (http://artax.karlin.mff.cuni.cz/r-help/library/RLastFM/html/00Index.html):

Suppose we want to search for Top Tracks in last 7 days or 3 months or 6 months or 12 months or overall, we can do it:

> user.getTopTracks(searchterm, period = NA, key = lastkey, parse = TRUE)

Here in above command, arguments are:
searchterm: Search term
period: overall | 7day | 3month | 6month | 12month (defaults to overall)
key: API key, defaults to global variable lastkey
parse: Parse the returned XML into a list? (defaults to TRUE)
Returns the values (result):

If parse = TRUE,
track: track name
artist: artist name
playcount: playcount of artist
artmbid: artist mbid
rank: rank of track, usually 1:50, except for ties

If parse = FALSE, a object of type "XMLInternalDocument" and "XMLAbstractDocument" 


> test6 = artist.getTopTracks("Red Hot Chilli Peppers")
> test7 = data.frame(test6)

Returns the following (top 50, showing a few in the pic below):



I hope this tutorial will be useful to those who are wishing to learn to play with APIs through R.

In this series, we will learning about Cluster Analysis in the part 2.

Keep practicing with the rest syntax.

Good Luck.