The common package is a lightweight package that contains solutions for commonly encountered problems when working in Base R.
Here is a list of the functions and a short explanation of each:
Normally, when working in Base R, it is necessary to quote variable names when passing them into a function or operator. For example, observe the R subset brackets:
# Variable names passed to subset are quoted <- mtcars[1:10 , c("mpg", "cyl", "disp")] dat # View results dat mpg cyl disp21.0 6 160.0 Mazda RX4 21.0 6 160.0 Mazda RX4 Wag 710 22.8 4 108.0 Datsun 4 Drive 21.4 6 258.0 Hornet 18.7 8 360.0 Hornet Sportabout 18.1 6 225.0 Valiant 360 14.3 8 360.0 Duster 24.4 4 146.7 Merc 240D 230 22.8 4 140.8 Merc 280 19.2 6 167.6Merc
Some Base R functions and almost all tidyverse functions use Non-standard Evaluation (NSE) when passing variable names. This style of evaluation allows the user to type variables without using quotation marks or other methods of resolution.
Picking up from the previous example, let’s now subset the
dat data frame created above using the
subset() function, which uses NSE:
# No quotes on "cyl" using subset() function dt <- subset(dat, cyl == 4) # View results dt # mpg cyl disp # Datsun 710 22.8 4 108.0 # Merc 240D 24.4 4 146.7 # Merc 230 22.8 4 140.8
v() function in the common package
is a quoting function. It allows you to use Non-Standard Evaluation
(NSE) even on functions that were not specifically written for NSE.
# Create a vector of unquoted names v1 <- v(mpg, cyl, disp) # Result is a quoted vector v1 #  "mpg" "cyl" "disp" # Variable names not quoted dat2 <- mtcars[1:10, v(mpg, cyl, disp)] # Works as expected dat2 # mpg cyl disp # Mazda RX4 21.0 6 160.0 # Mazda RX4 Wag 21.0 6 160.0 # Datsun 710 22.8 4 108.0 # Hornet 4 Drive 21.4 6 258.0 # Hornet Sportabout 18.7 8 360.0 # Valiant 18.1 6 225.0 # Duster 360 14.3 8 360.0 # Merc 240D 24.4 4 146.7 # Merc 230 22.8 4 140.8 # Merc 280 19.2 6 167.6
Base R provides sort and order functions that work adequately on vectors. For data frames, the options are more limited. In particular, if you want to sort a data frame by multiple columns, there are no functions in Base R to do it. The R documentation makes the following suggestion:
# Prepare data dat <- mtcars[1:10, 1:3] # Get sort order ord <- do.call('order', dat[ ,c("cyl", "mpg")]) # Sort data dat[ord, ] # mpg cyl disp # Datsun 710 22.8 4 108.0 # Merc 230 22.8 4 140.8 # Merc 240D 24.4 4 146.7 # Valiant 18.1 6 225.0 # Merc 280 19.2 6 167.6 # Mazda RX4 21.0 6 160.0 # Mazda RX4 Wag 21.0 6 160.0 # Hornet 4 Drive 21.4 6 258.0 # Duster 360 14.3 8 360.0 # Hornet Sportabout 18.7 8 360.0
In the above example, notice that a) there is no actual sorting function for data frames, and b) the method illustrated above provides no way to control the sort order of the variables involved. They are all sorted ascending.
sort.data.frame() function is an overload to the
sort() function that is tailored for data frames.
It allows you to sort by multiple columns, and control the sort
direction for each sort variable. Here is an example:
# Sort by cyl then mpg dat1 <- sort(dat, by = v(cyl, mpg)) dat1 # mpg cyl disp # Datsun 710 22.8 4 108.0 # Merc 230 22.8 4 140.8 # Merc 240D 24.4 4 146.7 # Valiant 18.1 6 225.0 # Merc 280 19.2 6 167.6 # Mazda RX4 21.0 6 160.0 # Mazda RX4 Wag 21.0 6 160.0 # Hornet 4 Drive 21.4 6 258.0 # Duster 360 14.3 8 360.0 # Hornet Sportabout 18.7 8 360.0 # Sort by cyl descending then mpg ascending dat2 <- sort(dat, by = v(cyl, mpg), ascending = c(FALSE, TRUE)) dat2 # mpg cyl disp # Duster 360 14.3 8 360.0 # Hornet Sportabout 18.7 8 360.0 # Valiant 18.1 6 225.0 # Merc 280 19.2 6 167.6 # Mazda RX4 21.0 6 160.0 # Mazda RX4 Wag 21.0 6 160.0 # Hornet 4 Drive 21.4 6 258.0 # Datsun 710 22.8 4 108.0 # Merc 230 22.8 4 140.8 # Merc 240D 24.4 4 146.7
While many data operations in R do not require control over the labels on a data frame, some types of programming do. Particularly in situations where you are sharing data between multiple people and groups, the column labels can provide valuable information about the data contained in a particular column.
Unfortunately, Base R does not supply an easy way to manipulate these
labels. The only approach is to use the
attr() function to
set the labels individually for each column. Like this:
labels.data.frame() function is an overload to the
labels() function that is specific to data frames.
The function allows you to set labels for an entire data frame using a
named list. Here is an example:
This function makes it much easier to set and retrieve labels on a
data frame. The labels make it easier for users to understand the data.
This function should be included in Base R, but for some reason is
Most programming languages provide a built-in concatenation operator.
R does not. Instead, it provides the
paste0() functions. While these functions do perform
concatenation adequately, it is sometimes more convenient to have an
# Concatenation using paste0() function paste0("There are ", nrow(mtcars), " rows in the mtcars data frame") #  "There are 32 rows in the mtcars data frame" # Concatenation using %p% operator "There are " %p% nrow(mtcars) %p% " rows in the mtcars data frame" #  "There are 32 rows in the mtcars data frame"
The common package contains an enhanced equality
operator. The objective of the
%eq% operator is to return a
TRUE or FALSE value when any two objects are compared. This enhanced
equality operator is useful for situations when you don’t want to check
for NULL or NA values, or care about the data types of the objects you
%eq% operator also compares data frames. The
comparison will include all data values, but no attributes. This
functionality is particularly useful when comparing tibbles, as tibbles
often have many attributes assigned by
Below is an example of several comparisons using the
%eq% infix operator:
# Comparing of NULLs and NA NULL %eq% NULL # TRUE NULL %eq% NA # FALSE NA %eq% NA # TRUE 1 %eq% NULL # FALSE 1 %eq% NA # FALSE # Comparing of atomic values 1 %eq% 1 # TRUE "one" %eq% "one" # TRUE 1 %eq% "one" # FALSE 1 %eq% Sys.Date() # FALSE # Comparing of vectors v1 <- c("A", "B", "C") v2 <- c("A", "B", "C", "D") v1 %eq% v1 # TRUE v1 %eq% v2 # FALSE # Comparing of data frames mtcars %eq% mtcars # TRUE mtcars %eq% iris # FALSE iris %eq% iris[1:50,] # FALSE # Mixing it up mtcars %eq% NULL # FALSE v1 %eq% NA # FALSE 1 %eq% v1 # FALSE
While it can be advantageous to have a comparison operator that does
not give errors when encountering a NULL or NA value, note that this
behavior can also mask problems with your code. Therefore, use the
%eq% operator with care.
Most programming languages provide a simple way to get the path of
the currently running program. This basic feature has been left out of
Sys.path() function aims to make up for the
# Get current path pth <- Sys.path() # View path pth #  "C:/packages/common/vignettes/common.Rmd"
Note that this function returns the full path of the currently
running program, including the file name and extension. This
functionality is different from
getwd(), which returns only
the current working directory.
As everyone knows, the R
round() function rounds to the
nearest even. For example:
However, humans and other software systems usually round 5 up. The reasons for R rounding the way it does are valid. Yet this difference in the way R rounds sometimes makes it difficult to compare R results to results from other software systems, particularly SAS®. It would be convenient if there were another rounding function that could be used when trying to compare R results to SAS®.
That is the purpose of the
roundup() function. Observe
the differences in output to what was shown above:
# Round up function r2 <- roundup(v1) # Rounds 5 up r2 #  1 2 3 4 5 6 7 8 9 10
Note that the function behaves differently when rounding negative values.
# Negate original vector v2 <- -v1 v2 #  -0.5 -1.5 -2.5 -3.5 -4.5 -5.5 -6.5 -7.5 -8.5 -9.5 # Rounding negative values r3 <- roundup(v2) # Rounds away from zero r3 #  -1 -2 -3 -4 -5 -6 -7 -8 -9 -10
As you can see, when dealing with negative numbers, the
roundup() function actually rounds down. “Round away from
zero” is the best description of this function. The rounding logic of
roundup() function matches SAS® software, and can be
used when comparing output between the two systems.
Sometimes you know the name of the file you are looking for, but do not know the exact location. It might be in the directory above your program, or it might be in the directory below. It could be one level up, or 3 levels up.
file.find() function provides an easy way to search
for files you are looking for. You tell the function where to start
searching from and what to look for, and it will begin looking in the
base directory. Once the base directory is searched, it will expand the
search above and below the base directory. The search routine will
continue expanding the search until it hits the limits imposed by the
down parameters. Here is an
The function will return a vector of full paths that meet the search criteria, and are within bounds of the search. If no file is found that meets the search criteria, the function returns a NULL.
Sometimes you have a data frame with many variables, and you need to
perform an operation on only some of them. The
function can help you subset these variable names. There are parameters
to define the search criteria, provide exclusions, and a beginning and
ending range to perform the search. Here are some simple examples:
# Prepare data dat <- mtcars # View names names(dat) #  "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb" # Get all names starting with "c" find.names(dat, pattern = "c*") #  "cyl" "carb" # Get all names starting with "c" or "d" find.names(dat, pattern = c("c*", "d*")) #  "cyl" "carb" "disp" "drat" # Get names starting with "c" or "d" from column 4 on find.names(dat, pattern = c("c*", "d*"), start = 4) #  "carb" "drat"
Base R functions that work with data frames are annoying in that they often drop any attributes assigned to data frame columns. Observe:
# Prepare sample dataset dat <- mtcars[ , 1:3] # Assign some labels labels(dat) <- list(mpg = "Miles Per Gallon", cyl = "Cylinders", disp = "Displacement") # View labels labels(dat) # $mpg #  "Miles Per Gallon" # # $cyl #  "Cylinders" # # $disp #  "Displacement" # Subset the data dat2 <- subset(dat, cyl == 4) # Labels are gone! labels(dat2) # list()
To get the attributes back, one must copy the attributes from the
original data frame to the subset data frame. That is what the
copy.attributes() function does. Picking up from the
example above, let’s now restore the attributes lost during the
There are many occasions when you need to create a superscript or subscript. The UTF-8 character set provides superscript and subscript versions of many commonly used characters. For example, the following code can be used to add a superscript ‘1’ to the front of a footnote string:
Remembering these UTF-8 codes, however, can be a challenge for most
subsc() functions look
up the superscript or subscript version of a normal character, without
having to remember or research the proper UTF-8 code.
Using these functions, we can therefore rewrite the above example as follows:
Here are a couple more examples:
Note that using the glue package, you can embed these functions directly in your character strings:
symbol() function retrieves symbols frequently used
in reports and documentation. This function is similar to the
subsc() functions in that it looks
up a UTF-8 character. Instead of providing a direct 1 to 1 translation,
however, it looks up the UTF-8 character based on a keyword.
For example, the ‘reg’ keyword looks up the registered trademark
symbol. The ‘ne’ keyword looks up the symbol for not equals. These
keyword names follow HTML conventions. The function supports keywords
for trademarks, currencies, mathematical symbols, logical symbols, Greek
letters, and more. See the
symbol() documentation for a
complete list of supported keywords.
It sometimes happens that you need to separate some strings by a certain number of blank spaces. This operation is often done in Base R as follows:
However, the above code is rather clumsy. The
function (plus the
%p% operator also found in this package)
can clean up this type of task for you significantly. Observe: