18) Create a Data Frame from Vectors.

This Blog entry is from the Data Structures section in Learn R.

For the great majority of Blog entries that follow in this document the Data Frame is clearly demonstrated to be the most important and ubiquitous data structure.  In its core a Data Frame is a list albeit with certain constraints.  A data frame can only make use of Vectors and Factors and furthermore these objects need to be of EXACTLY the same length. 

It can be helpful to thing of a Data Frame a being a hybrid of a Matrix and a List,  with a great deal more usability than a Matrix. It is worth remembering that owing to the presence of Factors and Vectors, this is to say different object types,  a matrix could not be used in all practicality.

To create a data frame of customers,  start by creating a vector of full names:

FullNames <- c("Donald Trump","Hilary Clinton"," Gary Johnson")
an-example-vector-to-be-used-in-a-data-frame-in-r.png

Run the line of script to console:

a-new-string-vector-created-and-written-to-r-console-for-data-frame.png

Repeat for a Vector of FullAges:

FullAges <- c(70,69,50)
a-numeric-vector-to-be-brought-together-in-a-data-frame-in-r.png

Run the line of script to console:

a-numeric-vector-written-out-to-r-console.png

Repeat for a Factor of FullGender, noting that the result of the c() function is being passed as the argument to thee factor() function:

FullGender <- factor(c("Male","Female","Male"))
a-factor-to-be-used-in-a-data-frame-in-r.png

Run the line of script to console:

a-factor-created-in-r-and-written-to-r-console.png

In a similar manner to both the c() function and the list() function,  the data.frame() fuction takes Vectors or Factors of the same length and combines them into a Data Frame.  As with the list() function it accepts a number of arguments in its advanced use,  however,  its most basic structure is the same as c().  To create a dataframe with default agurments type:

FullDataFrame <- data.frame(FullNames,FullAges,FullGender)
using-the-data-frame-function-to-create-a-data-frame-from-vectors.png

Run the line of script to console:

a-data-frame-created-bringing-together-factors-and-vectors-in-r-console.png

It can be observed that the data frame is now displayed in the environment pane under the data section and as such can be viewed in a similar manner to that set forth in procedure 27.

a-data-frame-written-to-the-environment-variables-in-rstudio.png

In this example a view is performed by a single click of the entry under the data section of the environment pane:

clickinng-on-the-data-frame-explodes-the-data-frame-in-the-same-way-as-a-matrix.png

In a similar manner to a Matrix,  the Data Frame is expanded into the grid viewer section of RStudio as a table.

13) Selecting from a Matrix.

This Blog entry is from the Data Structures section in Learn R.

As a matrix is made up of vectors, it is logical to expect it to bear some resemblance in the way selection from a matrix takes place.   All subscripting in a separate dimension when specified inside the [] square brackets, as separate arguments.  The first argument inside the square brackets relates to the row, the next the column.

To obtain the value in a given position of a matrix, in this case two down, three across, type:

OverspillMatrix[2,3]
selecting-a-variable-from-a-matrix-in-r.png

Run the line of script to console:

selecting-a-variable-from-a-matrix-written-to-r-console.png

It can be seen that the value 2 has been returned which corresponds to the position specified:

a-validation-of-the-value-returned-from-the-matrix-in-r-console.png

11) Create a Matrix of defined size with a Vector.

In Blog entries beforehand showed how to create a matrix using an intuitive method to bind vectors into columns and rows, comparing this to an Excel spreadsheet.  It is possible to create a matrix with a given specification then fill that specification with a single vector which over-spills the dimensions. 

The matrix() function is intended to take a single vector as an argument coupled with the dimensions (i.e. the number of rows and columns).  The single vector fills up this matrix by moving through each entry, downwards, in each column repeating the vector, should that vector not be long enough to fill up the matrix.

Start by creating a vector of six values by typing:

LongVector <- c(1,2,3,4,5,6)

1.png

Run the line of script to console:

2.png

Bearing in mind that the matrix will fill up column wise, make a matrix that is only three rows deep, while being four columns wide (i.e. nrow=3,ncol=4):

matrix(LongVector,nrow = 3,ncol = 4)
3.png

Run the line of script to console:

4.png

To view the matrix and specifically how the LongVector overlaid this matrix type:

OverspillMatrix
5.png

Run the line of script to console:

6.png

It can be seen that in moving column wise, when the vector runs out, it starts again until the matrix has been filled as per the dimensions.

10) Combine Vectors to make a Matrix with rbind.

This Blog entry is from the Data Structures section in Learn R.

Whereas cbind() brought vectors together as columns,   rbind() can bring vectors together as rows.  Start by creating two vectors in a script block:

Row1 <- c(1,2,3,4,5,6,7,8,9,10)
Row2 <- c(10,20,30,40,50,60,70,80,90,100)
Row3 <- c(100,200,300,400,500,600,700,800,900,1000)
creating-vectors-of-the-same-size-in-r-script.png

Run the script block to console:

creating-vectors-of-same-size-writen-to-r-console.png

To bind the vectors as rows use the rbind() function:

Matrix3Row <- rbind(Row1,Row2,Row3)
script-to-use-rbind-function-to-create-matrix-as-rows-in-r.png

Run the line of script to console:

matrix-created-from-rbind-written-to-r-console.png

The matrix can be viewed by typing:

Matrix3Row
script-to-write-out-matrix-created-by-rbind-in-r.png

Run the line of script to console:

matrix-created-by-r-bind-written-out-to-r-console.png

9) Viewing a Matrix

This Blog entry is from the Data Structures section in Learn R.

It can be observed that the matrix created in previous Blog entries has been written out to the console.  It was noted that there is a new section titled data in the environment pane,  under which the matrix is displayed. 

To expand the data into a tabbed grid,  simply click with the mouse on the Matrix3Col under the data section of the environment pane:

1.png

The tabbed grid will explode:

the-grid-view-in-rstudio.png

Note also that on clicking the matrix in the environment pane,  that a script command has actually sent to the console.  As such vieweing data in this manner can be invoked via a line of script. Using the script editor type:

View(Matrix3Col)
script-in-r-to-create-a-grid-view-in-rstudio.png

Run the line of script to the console:

showing-that-view-in-r-console-is-the-same-as-clicking-the-matrix-in-environment-variables.png