Category Archives: R Data Objects

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(), 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 Basics, R Data Objects | Comments Off on R Dates and Times

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 | Comments Off on Factors in R

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 | Comments Off on R Lists

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 | Comments Off on Local vs Global Objects

R Matrices

R matrices are two-dimensional vectors. A matrix starts with a vector and then adds dimension or dim information (e.g. rows and columns), which is stored in a memory slot for the matrix class.  The optional slot, .dimnames, holds row and column names (and their analogues for higher dimension arrays).

Initializing R Matrices

Initializing a matrix is similar to vector creation:

A unit matrix is easy to specify with the diag() function:

Posted in R Data Objects | Comments Off on R Matrices

R Data Import


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 Basics, R Data Import, R Data Objects | Comments Off on R Data Import

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 | Comments Off on R Data Objects

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 | Comments Off on R Vectors

Tidy Data Preparation

Package Dependencies

The core packages for tidy data preparation are listed below:

Of these, the tibble and tidyr packages are core to data consistency and preparation.1

Creating tibble Data

The tibble package provides a new data class for storing tabular data, the tibble. tibbles inherit the data.frame class, but improves 3 behaviors:

  • Subsetting – Always returns a new tibble, maintaining data consistency
Posted in Data Science, R Basics, R Data Objects, R Data Syntax | Comments Off on Tidy Data Preparation

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 Basics, R Data Objects, R Data Syntax | Comments Off on Data Formatting in R

Principles of Tidy Data

Introduction to Tidy Data

Despite the enormous amount of data available, there is surprisingly little alignment or information on how to create clean, consistent and easy to use data.

Human interface with data and code can benefit from some simple principles to facilitate repeatable research and results. The “tidy” approach to data requires that:

  • Data is structured consistently and reusable;
  • Code flow relies on simple function calls using the pipe;
Posted in Data, R Basics, R Data Objects, R Data Syntax, Scientific Computing | Comments Off on Principles of Tidy Data

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 | Comments Off on Data Frames in R

Geospatial Data and Mapping in R

I share slides presented at a recent meeting of  Doha R users on geospatial data and mapping in R .

Geospatial Data and Mapping in R (342 downloads)


Posted in Data Science, R Data Objects, R Data Syntax, R Programming, Spatial Analysis | Comments Off on Geospatial Data and Mapping in R

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 Basics, R Data Objects | Comments Off on Data Modes and Classes in R

Data Object Management

Data Object Management

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

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 Basics, R Data Objects | Comments Off on Data Object Management