Category Archives: R Data Objects

R Data Import

Introduction

Quantitative analysis depends on the ability to load and manage many different types of data and file formats.  There are many R data import functions. Some functions ship with base R and others can be found in R packages.

Data Available in R

R is pre-installed with many data sets in the datasets package, which is included in the base distribution of R. R datasets are automatically loaded when the application is started.  A list of all data sets in the package is obtained using the following command:

Posted in R Data Import, R Data Objects | Leave a comment

R Data Objects

Chapter Outline
Understanding R data objects is core to R programming.  The following sections provide practical insight into use of R data objects by type.

Vectors:            1 dimensional row and column vectors
Matrices:          2 dimensional rectangular data
Arrays:              N-dimensional data
Lists:                 Mixed mode data by component
Factors:            Qualitative data objects
Data Frames:   Mixed mode data by column

Posted in R Data Objects | Leave a comment

R Vectors

R vectors are commonly applied in mathematics, science and engineering.  A vector space is a structure formed by a collection of elements.  The most common interpretation of a vector is to represent location in the space of real numbers.  Similarly, vectors depict physical quantities that have both magnitude and direction, such as force or wind speed.

Posted in R Data Objects | Leave a comment

Data Concatenation and Coercion in R

Data concatenation and coercion are common operations in R.

Data Concatenation

The concatenate c() function is used to combine elements into a vector.

When elements are combined from different classes, the c() function coerces to a common type, which is the type of the returned value:

Posted in R Data Objects, R Data Syntax | Leave a comment

Data Formatting in R

There are a number of ways to accomplish data formatting in R.

Data Options in R

R supports a range of data formats and controls.  The options() function accesses the default settings R establishes at start-up.  Session options that can be changed from the command line include:

Each of these variables can be changed to modify R performance.  For more details on each element see the HTML help for the options() function.  A practical example is given below.

Posted in R Data Objects, R Data Syntax | Leave a comment

Data Frames in R

Arrays generalize the dimensional aspect of a matrix and assume only one data mode.  Data frames in R generalize the mode of a matrix and allow mode mixing.  Data frames with mode mixing are are the most widely used data objects in R.

Creating Data Frames in R

You can create data frames in R several ways:

  • importData() and read.table() both read data from an external file as a data.frame
Posted in R Data Objects | Leave a comment

Data Modes and Classes in R

In R, data modes and classes define the fundamental attributes and behavior of a data object.  For example, different modes and classes are handled differently by core functions like print(), summary(), and plot().

Data Object Modes

All data in R is an object and all objects have a “mode.”  The mode determines what type of information can be found within the object and how that information is stored.  Atomic “modes” are the basic building blocks for data objects in R.  There are 6 basic atomic modes:

Posted in R Data Objects | Leave a comment

Data Object Management

Data Object Management

The following functions are useful for data object management in R:

FunctionDescription
class()Identify the class of a named object.
colnames(); rownames()Retrieve or set the column or row names of an object.
dim()Retrieve or set the dimensions of a rectangular data object.
dimnames()Get or set the dim names of an object.
head()Returns the first n rows of a data object.
Posted in R Data Objects | Leave a comment

Data Sequences and Repetition in R

Data sequences and repetition are useful functions to define data objects, create new objects, control extractions or replacement, and manage function routines.

Data Sequences

The seq() function can be used several ways depending on its argument structure:

The first form generates the sequence from a number to a number and is identical to from:to:

The second form generates a sequence from:to with the step length by:

Posted in R Data Objects | Leave a comment

Data Sorting in R

Data sorting in R is simple and straightforward.  Key functions include sort() and order().   The variable by which sort you can be a numeric, a string or a factor variable.  Argument options also provide flexibility how missing values will be handled:  they can be listed first, last or removed.

Data Sorting Examples

It is also possible to sort in reverse order by using a minus sign ( – ) in front of the sort variable.  For example:

Posted in R Data Objects, R Programming | Leave a comment

R Data Subscripting

Intro to R Data Subscripting

R data subscripting is a key “motor skill” to extract data by row, column or element.  Subscripting can also extract data using logical conditions or pattern matching.  Subscripting is also used to assign values to data object elements.

The syntax for data subscripting can take several forms depending on data structure and data object type. Examples are provided below.

Posted in R Data Objects, R Programming | Leave a comment

R Dates and Times

Preprocessing work to maintain R dates and times requires synchronize of data and formats across data sources. R dates and times justify care and attention.

Current Date/Time in R

The function date(), Sys.date() and Sys.time() all return a character string of the current system data and time:

Each of these functions returns a slightly different result, which raises the obvious question how best to manage and format dates in large data objects?

Posted in R Data Objects, R Programming | Leave a comment

Factors in R

Categorical (e.g. qualitative) data are represented as factors in R.  Factors display as character strings (e.g. labels), but are stored as integers (e.g.  levels).

Creating Factors in R

Factors may be created by using the factor() or as.factor() function:

Note that it is not possible to assign labels to the factor levels within the function as.factor().

Another way to create factors in R is to split a data object into category groups and then call the factor() function:

Posted in R Data Objects, R Data Syntax | Leave a comment

R Lists

R lists are a general data object made up of components, where each component is itself a data object that can be any mode or type.  The length() of a list is the number of components.  Lists are very flexible and a convenient structure for packaging or storing different kinds of data in one object.  However, for large data, array structures are preferred based on operational run-times.  

Posted in R Data Objects | Leave a comment

Local vs Global Objects

Local vs global objects in R serve to distinguish temporary and permanent data.

Local Objects and Frames

Data objects assigned within the body of a function are temporary.  That is, they are local to the function only.  Local objects have no effect outside the function, and they disappear when function evaluation is complete.    

Posted in R Data Objects, R Programming | Leave a comment