Skip to content

Commit 3352be2

Browse files
committed
Add vignette that shows a before and after with rops
1 parent 6d3fc01 commit 3352be2

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

vignettes/rops.Rmd

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
---
2+
title: "Overview of Extended Binary Operators for Data Manipulation"
3+
author: "James J Balamuta"
4+
date: "`r Sys.Date()`"
5+
output: rmarkdown::html_vignette
6+
vignette: >
7+
%\VignetteIndexEntry{Overview of Extended Operators}
8+
%\VignetteEngine{knitr::rmarkdown}
9+
%\VignetteEncoding{UTF-8}
10+
---
11+
12+
# Overview
13+
14+
When writing functions or simply perform data analyses, sometimes you may
15+
wish for just that one additional operator or equality check that speeds up
16+
the process greatly. The goal behind this package is to provide such
17+
operators with as _minimal_ an overhead as possible.
18+
19+
# Package Installation
20+
21+
To install the package, please type:
22+
23+
```{r eval = FALSE}
24+
install.packages("rops")
25+
```
26+
27+
# Using the package
28+
29+
To use the package, please load it into the _R_ session by typing:
30+
31+
```{r}
32+
library("rops")
33+
```
34+
35+
# Coalescing Operators
36+
37+
## Null coalescing operator
38+
39+
```{r}
40+
# Null value
41+
x = NULL
42+
43+
# Before
44+
y = if(is.null(x)){ "Unset" } else { x }
45+
46+
# After
47+
y = x %??% "Unset"
48+
49+
y
50+
```
51+
52+
## Missing value (`NA`) coalescing operator
53+
54+
To impute values when "missingness" is detected, the `ifna()` provides a
55+
convenient interface.
56+
57+
```{r}
58+
x = c(1:5, NA, NA, 8, NA, 10)
59+
y = 1:10
60+
61+
# Before
62+
ifelse(is.na(x), x, y)
63+
ifelse(is.na(x), x, 3)
64+
65+
# After
66+
ifna(x, y)
67+
ifna(x, 3)
68+
```
69+
70+
# Equality Operators
71+
72+
Checking for exact equivalence in object can be done using an infix operator
73+
instead of relying upon `identical()`.
74+
75+
**NB** There are cases where checking with `all.equal()` is preferred.
76+
77+
```{r}
78+
x = y = 1:5
79+
x2 = x + 1
80+
81+
# Before
82+
identical(x, y)
83+
identical(x2, y)
84+
85+
# After
86+
x %==% y
87+
x2 %==% y
88+
89+
# Before
90+
!identical(x, y)
91+
!identical(x2, y)
92+
93+
# After
94+
x %!=% y
95+
x2 %!=% y
96+
```
97+
98+
99+
# Whole Numbers
100+
101+
Previously, using `is.integer()` would yield a check on the type of vector.
102+
Here, the `is_whole()` function seeks to check the state of
103+
all numbers individually in the vector regardless of whether they are
104+
`numeric` or `integer`.
105+
106+
```{r}
107+
x = c(1, 2, 3, 8.5)
108+
y = c(1L, 2L, 3L)
109+
110+
# Before
111+
is.integer(x)
112+
is.integer(y)
113+
114+
# After
115+
is_whole(x)
116+
is_whole(y)
117+
```
118+
119+
# Safe Sequence Generation
120+
121+
The colon operator (`:`) does not perform a check on whether the
122+
increment should be positive or negative. This leads to issues when
123+
iterating over an empty `vector` or `data.frame` using `1:length(obj)`.
124+
To avoid this, safe sequences `%:%` performs a check to make sure the
125+
sequence is _positive_. If it is not, then an empty integer vector is
126+
created to avoid having a loop run.
127+
128+
```{r}
129+
x = NULL
130+
131+
# Before
132+
1:length(x)
133+
134+
# After
135+
1 %:% length(x)
136+
```
137+
138+
# Not in Set
139+
140+
The not in set operator is a negation of the `%in%` operator that checks for
141+
whether an element belongs to the set.
142+
143+
```{r}
144+
x = 1:5
145+
set = 3:10
146+
147+
# Before
148+
!(x %in% set)
149+
150+
# After
151+
x %notin% set
152+
```

0 commit comments

Comments
 (0)