21) Loading .Rdata from file.

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

To fully demonstrate the process of loading objects from an RData file fully close down RStudio by clicking File,  then upon the menu expanding,  clicking Quit Session or by clicking on the close button in the top right hand corner:

means-to-save-an-rstudio-workspace.png

As expected from other procedures confirmation will be sought about the treatment of the current session.  Elect not to save the session by clicking "Don’t Save":

confirmation-not-to-save-r-workspace.png

Upon termination of RStudio,  simply reload:

no-workspace-when-r-studio-is-loaded.png

It can be seen that there are no objects loaded.  Assuming the working directory is unchanged, to load the objects saved in procedure 38, simply type:

load("Example.RData")
a-script-to-load-an-object-that-has-previously-been-saved-in-r.png

Run the line of script to console:

r-console-showing-the-object-to-have-been-loaded-without-error.png

The objects saved previously are promptly loaded and available in the environment pane of RStudio and by implication available for recall in scripts and \ or the console.

after-load-the-object-is-available-in-rstudio-environment-window.png

As R has several programmatic implementations,  such as R.net which is used for real-time invocation,  the saving and loading of R sessions provides a useful means to be able to deploy objects.

20) Saving .Rdata to file.

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

Machine learning is predominately a challenge of data abstraction – this is the shaping and molding of data – and presenting it to advanced machine learning algorithms on a commodity basis.  It follows that upon having spent time and effort creating an elaborate Data Frame,  it likely that it will need to be saved for future use (if only to avoid the computational expense of recreating it).

The save() function exists for the purpose of saving most objects that can be created and populated with data to a file in the working directory.  It is a very important part to deploying models on a real-time basis.

To save the Data Frame LabeledDataFrame and BucketList to a specified file by the name of "Example.RData":

save(LabeledDataFrame,BucketList,file = "Example.RData")
saving-an-object-script-in-r.png

Run the line of script to console:

saved-object-written-to-r-console.png

A file titled Example.RData is not written out to the Working Directory.  To remind the working directory:

getwd()
check-working-directory-script-in-r-for-save-validation.png

Run the line of script to console:

working-directory-written-out-to-r-console-for-save-validation.png

Having identified the working directory, navigate to the same in windows explorer:

object-saved-in-file-system-by-r.png

The saved file is clearly visible in this directory ready for real-time deployment or being reloaded to an R session.

19) Create a Data Frame from Names and stringsAsFactors.

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

As introduced previously the data.frame() function, not unlike the list() function, has more flexibility to be able to create objects than the c() function.   As seems intuitive it is possible to specify names explicitly rather than take the names of the Vectors by default.  There is an argument to the data.frame() function that can ease the burden of creating factors upon detection of character vectors in the for of the stringsAsFactors switch (although it is not always sensible to use it in the case of numeric prediction focus). 

To create a Data Frame with specific names and disabling stringsAsFactors:

LabeledDataFrame <- data.frame(data.frame(ExampleFullNames = FullNames,ExampleFullAges = FullAges,ExampleFullGender = FullGender,stringsAsFactors = FALSE))
a-script-in-r-to-add-a-names-to-a-data-frame.png

Run the line of script to console:

new-data-frame-with-names-written-to-r-console.png

Return the Data Frame by typing:

a-script-to-write-out-a-labeled-data-frame-in-r.png

Run the line of script to console:

a-named-data-frame-written-out-to-the-r-console.png

It can be observed that the column names have been correctly specified.  Unless a factor has been specifically allocated it can be trusted that other character Vectors,  such as FullName in this example, will not be transposed to factors automatically.

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.

17) Sub setting and referencing objects with a name

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

The most useful and common way to navigate a list is by referencing the entry in the list  by name then sub setting the object thereafter.  The approach of referencing list objects by name,  then sub setting thereafter can serve to make a distinction between a list and a vector in day to day use.

The list created beforehand has several objects with the names TempsExample, GenderExample,MatrixExample and VectorExample.  Start by returning a vector object by name:

BucketList$VectorExample
a-list-and-the-dollar-in-r-to-return.png

Run the line of script to console:

a-vector-contained-in-a-list-written-to-the-r-console.png

It can be seen that the object stored under the name "VectorExample" is a labeled Vector.  As this is a Vector,  it is possible to further subset this using techniques outlined in procedure 25.  For example,  to return Tom’s age from the Vector,  type:

BucketList$VectorExample["Tom"]
showing-how-sub-set-can-work-on-a-list-as-if-it-were-a-vector-in-r.png

Run the line of script to console:

a-variable-returned-from-a-vector-from-a-list-in-r.png

It can be observed that the vector was drawn from the list by name, then subset as is customary for a vector.