Aggregates in R – one of the most powerful tools you can ask for.

Aggregates in R – one of the most powerful tools you can ask for.

Satyam Singh ChauhanBlockedUnblockFollowFollowingMar 11“Play with the Data”.

Data Visualization is not just about colors and graphs.

It’s about exploring the data and visualizing the right thing.

SourceWhile playing with the data the most powerful tool that comes handy is Aggregates.

Aggregates is just the type of transformation that we apply to any given data.

We have 11 aggregate function available to us.

avgAverage of all numeric values is calculated and is returned.

countFunction count returns total number of items in each group.

firstThe first value of each group is returned by the function first.

lastThe last value of each group is returned by the function last.

maxThe max value of each group is returned by the function max.

It is very helpful to identify outliers as well.

medianThe median of all numeric values for the mentioned group is returned by the function median.

minThe min value of each group is returned by the function min.

It is very helpful to identify outliers as well.

modeThe mode of all numeric values for the mentioned group is returned by the function mode.

rmsRoot Mean Square, rms value for all numeric values in the group is returned by the fucntion rms.

sttdevStandard Deviation of all Numeric values given in the group is returend by the function stddev.

sumSum of all the numeric values is returned by the function sum.

Basic ExamplesBasic Visual Scatter plot using aggregate function — sum#Include the Librarylibrary(plotly)#Store the graph in one variable to make it easier to manipulate.

p <- plot_ly( type = 'scatter', y = iris$Petal.

Length/iris$Petal.

Width, x = iris$Species, mode = 'markers', marker = list( size = 15, color = 'green', opacity = 0.

8 ), transforms = list( list( type = 'aggregate', groups = iris$Species, aggregations = list( list( target = 'y', func = 'sum', enabled = T ) ) ) ))#Display the graphpWhat does this mean?Function sum as mentioned above, calculates the sum of each group.

Thus, here the groups are categorized as species.

Code uses Iris Data Set which consist three different species, setosa, veriscolor, virginica.

For each species there are 50 observations in the data set.

This data set is available in R (built-in) and can be loaded directly.

 “iris” and “iris3” – two data sets are available.

You can choose any one of them to run these codes.

The Data-Set used in this article is “iris”.

Fig.

1 Sum of Petal LengthWhat does this code exactly do?This code uses the function sum and calculates the sum of all the Petal.

Length of each group respectively.

Then, the calculated sum is plotted on the x-y axis.

Where the x-axis is Species, while the y-axis shows the Summation.

From this graph, we can get an idea that the petal size of setosa is least as the sum is least but it’s not a conclusive evidence.

To get conclusive evidence we can use the function avg.

 The function sum is very suitable for almost all the data set.

for example one of the best places where this can be used is in Population Data Set.

In world population data-set we can aggregate countries according to continents and find sum of all the population of the countries in it.

Most used function — avg#Include the Librarylibrary(plotly)#Store the graph in one variable to make it easier to manipulate.

q <- plot_ly( type = 'bar', y = iris$Petal.

Length/iris$Petal.

Width, x = iris$Species, color = iris$Species, transforms = list( list( type = 'aggregate', groups = iris$Species, aggregations = list( list( target = 'y', func = 'avg', enabled = T ) ) ) ))#Display the graphqWhat does this mean?The iris data-set contains two columns for Petals, Petal.

Width and Petal.

Length.

Further, It can be used to calculate the average of the ratio of Petal.

Length & Petal.

Width.

Fig.

2 Average ratio of Petal Length to Petal WidthWhat does this code exactly do?For each observation ratio of Petal.

Length to Petal.

Width is calculated before the average of all the gained value is plotted.

As we can observe from this Bar Plot, Setosa has the max ratio with near-ratio of 7, which shows that Petal length in Setosa is 7 times larger than its’s width.

While on the other hand, virginica has the least ratio with nearly 3 times the width.

This function is very flexible and it can be used very wisely to get the best result.

For example, if we consider some other data-set of Population then we can calculate the average birth is to death ratio of each country.

Let’s use all the functions in one graph.

Now we’re going to plot a scatter plot for each category and we’re going to use all the function, so to this we will add a button to the graph from which we can select the desired function to make our work easier and facilitate us to get results quicker.

Aggregation of all functions — all function in one-graph#Include the Librarylibrary(plotly)#Store the graph in one variable to make it easier to manipulate.

s <- schema()agg <- s$transforms$aggregate$attributes$aggregations$items$aggregation$func$valuesl = list()for (i in 1:length(agg)) { ll = list(method = "restyle", args = list('transforms[0].

aggregations[0].

func', agg[i]), label = agg[i]) l[[i]] = ll }p <- plot_ly( type = 'scatter', x = iris$Species, y = iris$Sepal.

Length / iris$Sepal.

Width, mode = 'markers', marker = list( size = 20, color = 'orange', opacity = 0.

8 ), transforms = list( list( type = 'aggregate', groups = iris$Species, aggregations = list( list( target = 'y', func = 'avg', enabled = T ) ) ) )) %>%layout( title = '<b>Plotly Aggregations by Satyam Chauhan</b><br>use dropdown to change aggregation<br><b>Sepal ratio of Length to Width</b>', xaxis = list(title = 'Species'), yaxis = list(title = 'Sepal ratio: Length/Width'), updatemenus = list( list( x = 0.

2, y = 1.

2, xref = 'paper', yref = 'paper', yanchor = 'top', buttons = l ) ))#Display the graphsWhat does this mean?A list is made where all the function attributes of aggregation are stored.

We make use of this function to experiment with all the functions of Aggregations in R.

Few of the graphs with different examples are shown below.

Fig.

3 Illustrates the function mode.

What does this code exactly do?First, a list is created as mentioned earlier in which all the function is stored.

 After the list is made, the y-axis is set to the ratio of Sepal.

Length to Sepal.

Width and x-axis is set as Species.

After calculating the ratio, the function transform is called in which the func = ‘avg’ is mentioned for just starting phase.

When we run this code and select the function ‘mode’ we get Fig.

3, which shows that mode of setosa is the least among the three at around 1.

4.

Mode tells that the ratio 1.

4 is repeated the most number of times or that value is most likely to be sampled.

The different pattern we saw here is that the highest value most likely to be sampled is from the category veriscolor having mode near to 2.

2.

Fig.

4 Left Figure: Illustrates the change in Sepal ratio of Length to Width Right Figure: Illustrates the root mean square (rms) value of the GraphIn the Fig.

4, the change of ratio of Sepal Length to Sepal Width is plotted and we get very different results compared to the rest of the graphs.

We observe the change of Setosa and Virginica to be same and Positive while the change of ratio by Species veriscolor is almost negative and 3 times the change of the setosa and virginica.

On the other hand, the Right figure shows the rms values of each species.

We can easily see that the species veriscolor and virginica have almost same value which is significantly greater than the rms value of setosa.

ConclusionAggregation function is one of the most powerful tools the Professionals, Enthusiasts , Students or Developers can ask for.

It can provide you the patterns, results that you wouldn’t expect.

To analyse the data visually, you have to play with data and to do that we need to manipulate and transform the data.

Aggregation function does that for you, It’s is one of the most widely used function in transform.

This article is just a start.

Explore more and Apply more.

That’s what explorers do.

.. More details

Leave a Reply