# Data Analysis in RUby: Basic Data Manipulation and Plotting

daru (Data Analysis in RUby) is a ruby gem for performing various data analysis and manipulation tasks in Ruby. It draws inspiration from pandas (python) and aims to be completely cross-compatible between all ruby implementations (MRI/JRuby etc.) yet leverage the individual benefits that each interpreter offers (for example the speed of C in MRI), while offering a simple and powerful API for data analysis, manipulation and visualization.

In this first article on daru, I will show you some aspects of how daru handles data and some operations that can be performed on a real-life data set.

## Getting Started

daru consists of two major data structures:

• Vector - A named one-dimensional array-like structure.
• DataFrame - A named spreadsheet-like two-dimensional frame of data.

A Vector can either be represented by a Ruby Array, NMatrix(MRI) or MDArray(JRuby) internally. This allows for fast data manipulation in native code. Users can change the underlying implementation at will (demonstrated in the next blog post).

Both of these can be indexed by the Daru::Index or Daru::MultiIndex class, which allows us to reference and operate on data by name instead of the traditional numeric indexing, and also perform index-based manipulation, equality and plotting operations.

#### Vector

The easiest way to create a vector is to simply pass the elements to a Daru::Vector constructor:

Since no name has been specified, the vector is named nil, and since no index has been specified either, a numeric index from 0..4 has been generated for the vector (leftmost column).

A better way to create vectors would be to specify the name and the indexes:

This way we can clearly see the quantity of each item possesed by Sherlock.

Data can be retrieved with the [] operator:

#### DataFrame

A basic DataFrame can be constructed by simply specifying the names of columns and their corresponding values in a hash:

You can also specify an index for the DataFrame alongwith the data and also specify the order in which the vectors should appear. Every vector in the DataFrame will carry the same index as the DataFrame once it has been created.

daru will also add nil values to vectors that fall short of elements.

Creating a DataFrame by specifying Vector objects in place of the values in the hash will correctly align the values according to the index of each vector. If a vector is missing an index present in another vector, that index will be added to the vector with the corresponding value set to nil.

A DataFrame can be constructed from multiple sources:

• To construct by columns:
• Array of hashes - Where the key of each hash is the name of the column to which the value belongs.
• Name-Array Hash - Where the hash key is set as the name of the vector and the data the corresponding value.
• Name-Vector Hash - This is the most advanced way of creating a DataFrame. Treats the hash key as the name of the vector. Also aligns the data correctly based on index.
• Array of Arrays - Each sub array will be considered as a Vector in the DataFrame.
• To construct by rows using the .rows class method:
• Array of Arrays - This will treat each sub-array as an independent row.
• Array of Vectors - Uses each Vector in the Array as a row of the DataFrame. Sets vector names according to the index of the Vector. Aligns vector elements by index.

## Handling Data

Now that you have a basic idea about representing data in daru, lets see some more features of daru by loading some real-life data from a CSV file and performing some operations on it.

For this purpose, we will use iruby notebook, with which daru is compatible. iruby provides a great interface for visualizing and playing around with data. I highly recommend installing it for full utilization of this tutorial.

Let us load some data about the music listening history of one user from this subset of the Last.fm data set:

As you can see the timestamp field is in a somewhat non-Ruby format which is pretty difficult for the default Time class to understand, so we destructively map time zone information (IST in this case) and then change every timestamp string field into a Ruby Time object, so that operations on time can be easily performed.

Notice the syntax for referencing a particular vector. Use ‘row’ for referencing any row.

#### Basic Querying

A bunch of rows can be selected by specifying a range:

df.row[900..923]

#### Data Analysis

Lets dive deeper by actually trying to extract something useful from the data that we have. Say we want to know the name of the artist heard the maximum number of times. So we create a Vector which consists of the names of the artists as the index and the number of times the name appears in the data as the corresponding values:

To get the maximum value out of these, use #max_index. This will return a Vector which has the max:

count.max_index

#### Plotting

daru uses Nyaplot for plotting, which is an optional dependency. Install nyaplot with gem install nyaplot and proceed.

To demonstrate, lets find the top ten artists heard by this user and plot the number of times their songs have been heard against their names in a bar graph. For this, use the #sort function, which will preserve the indexing of the vector.

More examples can be found in the notebooks section of the daru README.