Category Archives: R Data Objects

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 (109 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:

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

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

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

R Data Subscripting

Intro to R Data Subscripting

Data subscripting in R is a key “motor skill” to extract data by row, column or element.  Subscripting is achieved using numeric, character, 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 Basics, R Data Objects | Comments Off on R Data Subscripting

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