### Login

### Today in Energy

### Tags

Animation (3) Data (12) Data Science (12) Distributions (1) Economics (5) Engineering (1) Faster R (2) GDAL (5) ggplot2 (19) Git/GitHub (3) LaTeX (12) Linux (9) Misc Tricks (3) Modeling (6) Projects (1) R Basics (17) R Colors (9) R Data Import (6) R Data Objects (18) R Data Syntax (11) R Graphics (20) R Packages (3) R Programming (26) Scientific Computing (4) Spatial Analysis (6) Ubuntu (1) Web Scrapping (2) Website (5)### Archives

# Category Archives: R Data Syntax

## Data Formatting in R

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

**Data Options 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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
> names(options()) [1] "add.smooth" "bitmapType" "browser" [4] "browserNLdisabled" "check.bounds" "continue" [7] "contrasts" "defaultPackages" "demo.ask" [10] "device" "device.ask.default" "digits" [13] "dvipscmd" "echo" "editor" [16] "encoding" "example.ask" "expressions" [19] "help_type" "help.search.types" "help.try.all.packages" [22] "HTTPUserAgent" "internet.info" "keep.source" [25] "keep.source.pkgs" "locatorBell" "mailer" [28] "max.print" "menu.graphics" "na.action" [31] "nwarnings" "OutDec" "pager" [34] "papersize" "pdfviewer" "pkgType" [37] "printcmd" "prompt" "repos" [40] "rl_word_breaks" "scipen" "show.coef.Pvalues" [43] "show.error.messages" "show.signif.stars" "str" [46] "str.dendrogram.last" "stringsAsFactors" "texi2dvi" [49] "timeout" "ts.eps" "ts.S.compat" [52] "unzip" "useFancyQuotes" "verbose" [55] "warn" "warning.length" "width" |

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.

Read more...

*(202 words, estimated 48 secs reading time)*
Posted in R Basics, R Data Objects, R Data Syntax
Comments Off on Data Formatting in R

## Data Infix Operators in R

*Intro to Infix Operators in R*

*Intro to Infix Operators in R*

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

Read more...

*(322 words, 1 image, estimated 1:17 mins reading time)*
Posted in R Basics, R Data Syntax
Comments Off on Data Infix Operators in R

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

*Creating Factors in R*

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

1 2 3 4 5 6 7 8 9 10 |
# Create data object using factor() > age <- factor(c(1, 1, 2, 2, 1, 3, 1, 2), labels = c("20-35yrs", "35-55yrs", "55+yrs")) > age [1] 20-35yrs 20-35yrs 35-55yrs 35-55yrs 20-35yrs 55+yrs 20-35yrs 35-55yrs # Create data object using as.factor() > age <- c("20-35yrs", "20-35yrs", "35-55yrs", "35-55yrs", "20-35yrs", "55+yrs", "20-35yrs", "35-55yrs") > age <- as.factor(age) > age [1] 20-35yrs 20-35yrs 35-55yrs 35-55yrs 20-35yrs 55+yrs 20-35yrs 35-55yrs |

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:

Read more...

*(368 words, estimated 1:28 mins reading time)*
Posted in R Data Objects, R Data Syntax
Comments Off on Factors in R

## Tidy Data Transformations

**Package Dependencies**

**Package Dependencies**

The core packages for tidy data transformations are listed below:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Data format/shape library(tibble) # simple data.frames library(tidyr) # data cleaning/reshaping # Data transforms library(dplyr) # data transforming library(forcats) # data factor mgmt library(lubridate) # data/time objects library(hms) # time-of-day values library(stringr) # string mgmt # Programming library(purrr) # functional programming tools library(purrrlyr) # intersection of purrr and dplyr library(magrittr) # pipe operators |

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.

Read more...

*(1093 words, 8 images, estimated 4:22 mins reading time)*
Posted in Data Science, R Basics, R Data Syntax, R Programming
Comments Off on Tidy Data Transformations

## R Data Syntax

The following pages introduce the fundamentals of R data syntax for program scripting and quantitative data analysis.

- Data Object Modes and Classes
- Data Object Management
- Data Formatting
- Dates and Date Formats
- Data Subscripting
- Data Infix Operators
- Data Expressions
- Data Concatenation and Coercion
- Data Sequences and Repetition
- Data Sorting
- Data Distributions
- Regular Expressions

Permanent link to this post (55 words, estimated 13 secs reading time)

Posted in R Data Syntax
Comments Off on R Data Syntax

## Regular Expressions (RegEx) in R

In computing, a regular expression (abbreviated regexp) is a sequence of characters that forms a search pattern, mainly for use in pattern matching with strings. The patterns are often a combination of text abbreviations, metacharacters, and wild cards. Regular expressions are used for searching for objects, doing extractions, or find/replace operations. The use of regular expressions offers convenience and can have powerful impact on data or object management.

**regexp ***Functions in R*

*Functions in R*

Functions in R for regular expressions include:

Read more...

*(913 words, estimated 3:39 mins reading time)*
Posted in R Data Syntax
Comments Off on Regular Expressions (RegEx) in R

## Tidy Data Preparation

**Package Dependencies**

**Package Dependencies**

The core packages for tidy data preparation are listed below:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Data format/shape library(tibble) # simple data.frames library(tidyr) # data cleaning/reshaping # Data transforms library(dplyr) # data transforming library(forcats) # data factor management library(lubridate) # data/time objects library(hms) # time-of-day values library(stringr) # string mgmt # Programming library(purrr) # functional programming tools library(purrrlyr) # intersection of purrr and dplyr library(magrittr) # pipe operators |

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

*Creating *tibble** Data**

*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

Read more...

*(751 words, 4 images, estimated 3:00 mins reading time)*
Posted in Data Science, R Basics, R Data Objects, R Data Syntax
Comments Off on Tidy Data Preparation

## Principles of Tidy Data

**Introduction to 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;

Read more...

*(645 words, 1 image, estimated 2:35 mins reading time)*
Posted in Data, R Basics, R Data Objects, R Data Syntax, Scientific Computing
Comments Off on Principles of Tidy Data

## 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 (169 downloads)

Permanent link to this post (30 words, 1 image, estimated 7 secs reading time)

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

## Data Expressions in R

*Data Expressions*

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

Function | Description | Comment |
---|---|---|

abs() | Absolute value | n/a |

approx() | Linear interpolation of points | n/a |

asin(); acos(); atan() | Inverse trigonometric functions | n/a |

asinh(); acosh(); atanh() | Inverse hyperbolic functions | n/a |

ceiling() | Round up to nearest integer | Impacts stored precision |

Read more...

*(234 words, estimated 56 secs reading time)*
Posted in R Basics, R Data Syntax
Comments Off on Data Expressions in R

## Data Concatenation and Coercion in R

Data concatenation and coercion are common operations in R.

*Data Concatenation*

*Data Concatenation*

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

1 2 3 4 5 |
> c(T, F, T) [1] T F T > c(8.3, 9.2, 11) [1] 8.3 9.2 11.0 |

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

1 2 3 4 |
> x <- c(100, "A", TRUE, as(1, "complex")) > x [1] "100" "A" "TRUE" "1+0i" > class(x) [1] "character" |

Read more...

*(256 words, estimated 1:01 mins reading time)*
Posted in R Basics, R Data Syntax
Comments Off on Data Concatenation and Coercion in R