Category Archives: R Basics

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

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 Basics, R Data Syntax | Comments Off on Data Concatenation and Coercion in R

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

Project Reporting with RMarkdown


RMarkdown  provides an authoring system for project and data science reporting.  RMarkdown is a core component of the RStudio IDE.  It braids together narrative text with embedded chunks of R code.  The  R code serves to demonstrate the model concepts in the text.  RMarkdown  produces elegantly formatted document output, including  publication quality data plots and tables.

Posted in Data Science, LaTeX, R Basics, R Programming, Scientific Computing | Comments Off on Project Reporting with RMarkdown

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 Infix Operators in R

Intro to Infix Operators in R

postfixInfix operators in R are unique functions and methods that facilitate basic data expressions or transformations.  

Infix refers to the placement of the arithmetic operator between variables.  For example, an infix operation is given by (a+b), whereas prefix and postfix operators are given by (+ab) and (ab+), respectively.  

The types of infix operators used in R include functions for data extraction, arithmetic, sequences, comparison, logical testing, variable assignments, and custom data functions. 

Posted in R Basics, R Data Syntax | Comments Off on Data Infix Operators 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 Basics

The R Kernel

The R kernel is compromised of scripts written in the R and C programming languages.  It includes a set of core function libraries, an interpreter for machine interface to run R scripts or functions, and a set of powerful graphical devices.  In total, these elements are referred to as base R.

Posted in R Basics | Comments Off on R Basics

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

Tidy Data Transformations

Package Dependencies

The core packages for tidy data transformations are listed below:

The dplyr package is by far the most important of the packages in the “tidyverse” for data transformation and manipulation.1  Verb-based functions are one of the advantages of the package.  The syntax is much easier to use when compared to the cryptic syntax of base R.

Posted in Data Science, R Basics, R Data Syntax, R Programming | Comments Off on Tidy Data Transformations

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

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 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 Expressions in R

Data Expressions

The following list defines data expressions in R that are used to compute basic numerical results for scalars, vectors, and rectangular data objects.  Scroll through the table to see all functions:

abs()Absolute valuen/a
approx()Linear interpolation of pointsn/a
asin(); acos(); atan()Inverse trigonometric functionsn/a
asinh(); acosh(); atanh()Inverse hyperbolic functionsn/a
ceiling()Round up to nearest integerImpacts stored precision
Posted in R Basics, R Data Syntax | Comments Off on Data Expressions 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