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:


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


Upon termination of RStudio,  simply reload:


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:


Run the line of script to console:


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.


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

Run the line of script to console:


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


Run the line of script to console:


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


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

Run the line of script to console:


Return the Data Frame by typing:


Run the line of script to console:


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

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.

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:


Run the line of script to console:


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:


Run the line of script to console:


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