CodeInfoNet

Learn programming on the go!

Home » Matrix operations in R using Basketball data set

Matrix operations in R using Basketball data set

In this article we will learn about various matrix operations in R using Basketball data set.

Note:

For this particular blog and few other blogs we are going to use the data set prepared by www.superdatascience.com.

Before we start anything related to matrix operations, Let’s first get familiar with the data set we are going to use as an input data.

So, we will be using the Basketball data set.

You don’t necessarily need to be aware of all the stuffs related to basketball. This is just a data set containing the information about the players, their performance over the years and their stats.

Loading Data Set:

To load the data set into your work space, you need to go through few steps. Here is the article showing how to use the basketball data set. Click on below link:

How to get basketball data set?

Hopefully you have downloaded the data set into your work space.

Matrix Operations

So, in our previous articles we have already seen some operations on vectors such as adding two vectors, subtracting one vector from other, multiplication between the vectors.

Similarly, can we perform these operations on a matrix? The answer to this question is “Yes! Of course we can!!”

Are you excited to know what operations we can perform on matrices? Let’s see one by one:

After loading the data set into work space, you will see that there are multiple matrices such as Games, FieldGoals, Salaries, FieldGoalAttempts etc.

Below are some simple functions to make you people comfortable with the matrix operations with the use of this particular data set.

Getting Row/Column Names

Let’s see what row names and column names the Games matrices has.

We use below simple code:

Rownames(Games)
Colnames(Games) 

You can try these steps for any matrix for sure. As usual I would suggest you to try by your own.

We already know how to get the value of specific column in a specific row.

If you want to find out how many games a player has played in a particular year, you can use following syntax:

Games[rowname, columnname]
e.g. Games[“LeBronJames”, “2012”]

Matrix Division

Consider we want to find out how many field goals every player has scored in each game that they have played every year.

Let’s see how to do it:

We need to move to FieldGoals matrix. Write FieldGoals and execute to get the data in FieldGoals matrix

If we were doing it with any other programming language, we would need to loop through both of these matrices i.e Games and Fieldgoals.

But R has made it so simple that we just need to write a single line to get the expected result

We need to divide these two matrices to get number of scored goals.

Here is how : FieldGoals/Games

And to make it more readable you can round it to 1 decimal.

round(FieldGoals/Games,1)

Same operation can be performed to calculate the minutes played by each player every year.

I will keep this task for my readers.

Likewise, any operation such as adding matrices, subtracting one from another, multiplying and dividing matrices can be performed with matrices in R.

Hence, we started with matrix operations. Keep reading for simple and amazing articles like this.

Keep me posted if you have any query. Your advice is priceless. Please provide any feedback regarding the quality of explanation or the complexity of content using below feedback form.

Feel free to comment or reach us directly via mail. Also you can contact us by Facebooklinkedintwitterinstagram any of the platform you feel comfortable with.

Sumit Rajguru

Back to top