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")

Run the line of script to console:


Repeat for a Vector of FullAges:

FullAges <- c(70,69,50)

Run the line of script to console:


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"))

Run the line of script to console:


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)

Run the line of script to console:


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.


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


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:


Run the line of script to console:


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


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)


Run the line of script to console:


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)

Run the line of script to console:


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


Run the line of script to console:


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)

Run the script block to console:


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

Matrix3Row <- rbind(Row1,Row2,Row3)

Run the line of script to console:


The matrix can be viewed by typing:


Run the line of script to console:


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:


The tabbed grid will explode:


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:


Run the line of script to the console: