# Creating a Matrix¶

We've learned about vectors which allow us to store indexed elements. A matrix will allow us to have a 2-dimensional data structure which contains elements consisting of the same data type.

Before we talk about the Matrix, we should show a quick tip for quickly creating sequential numeric vectors, you can use the colon notation from slicing to create sequential vectors:

In [6]:

    1:10

Out[6]:

1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10

In [8]:

    v <- 1:10
    v

Out[8]:

1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10

Great! Now, to create a matrix in R, you use the matrix() function. We can pass in a vector into the matrix:

In [10]:

    matrix(v)

Out[10]:

1
2
3
4
5
6
7
8
9
10

Notice how the output is displayed. Here we have a two-dimensional matrix which is 10 rows by 1 column. Now what if we want to specify the number of rows? We can pass the parameter/argument into the matrix function called nrow which stands for number of rows:

In [13]:

    matrix(v,nrow=2)

Out[13]:

1 | 3 | 5 | 7 | 9
2 | 4 | 6 | 8 | 10

Now we have a 2 by 5 matrix. Notice that the nrow argument allows this to happen. But how do we decide the fill order? We could have filed columns first (as we did above) or filled out the rows first insequential order. The byrow argument allows you to specify whether or not you want to fill out the matrix by rows or by columns. For example:

In [11]:

    matrix(1:12,byrow = FALSE,nrow=4)

Out[11]:

1 | 5 | 9
2 | 6 | 10
3 | 7 | 11
4 | 8 | 12

In [14]:

    matrix(1:12, byrow=TRUE, nrow=4)

Out[14]:

1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
10 | 11 | 12

In [5]:

    v

Out[5]:

1. 1
2. 2
3. 3
4. 4
5. 5
6. 6
7. 7
8. 8
9. 9
10. 10
11. 11
12. 12

## Creating Matrices from Vectors¶

We can combine vectors to later input them into a matrix. For example imagine the following vectors below of stock prices:

In [15]:

    # not real prices
    goog <- c(450,451,452,445,468)
    msft <- c(230,231,232,236,228)

In [16]:

    stocks <- c(goog,msft)

In [20]:

    stock.matrix <- matrix(stocks,byrow=TRUE,nrow=2)

In [21]:

    stock.matrix

Out[21]:

450 | 451 | 452 | 445 | 468
230 | 231 | 232 | 236 | 228

## Naming Matrices¶

Now that we have our matrix, it would be nice to name the rows and columns for reference. We can do this similarly to the names() function for vectors, but in this case we define colnames() and rownames(). So let's name our stock matrix:

In [22]:

    days <- c('Mon','Tue','Wed','Thu','Fri')
    st.names <- c('GOOG','MSFT')

In [23]:

    colnames(stock.matrix) <- days
    rownames(stock.matrix) <- st.names

In [24]:

    stock.matrix

Out[24]:

| Mon | Tue | Wed | Thu | Fri
--- | --- | --- | --- | --- | ---
GOOG | 450 | 451 | 452 | 445 | 468
MSFT | 230 | 231 | 232 | 236 | 228

Looking good! Now we are starting to really build up something powerful here, you should begin to be reminded of a spreadsheet!

That's is for the basics of creating a matrix! In the next lecture we will be building off this matrix of stock prices, so keep the code for future reference!