Common Uncommon Notations that Confuse New R Coders

Here are a few of the more commonly used notations found in R code and documentation that confuse coders of any skill level who are new to R.

Be aware that any variable name that begins with a period character (.) is usually hidden from view, so won’t be seen in the Environment pane in RStudio or listed when you invoke

  ls()

unless you specify

  ls(all.names = TRUE)

Notation Meaning
. The period can be used as a character in any variable name. Unlike in Java and other languages, it does not indicate object hierarchy. a.variable.name is just that: a variable name. There is no object called a with a property called variable with a property called name. There is just a single name, a.variable.name, that you may have associated with an object. You’ll sometimes see model.1 and model.2 style naming used. Again, there is no model object in such cases.
. The single period can be the entire name of a variable. It’s often found in association with the magrittr pipe operator %>% and is the name used to denote “the otherwise anonymous object being passed in from the left-hand side (LHS)”. You typically see it used when you need to pass the piped object into base-R or other non-tidyverse functions. Normally, tidyverse functions are written to accept a data object as their first parameter and the pipe operator passes the result of the LHS implicitly as the first argument, but base-R functions might expect the data object in any parameter position.
...

Known as “dot-dot-dot”, “ellipses”, or “three dots”—difficult to search for, unless you know those names for it. This is another variable name. It can only be used as a formal parameter to a function and is often simply passed along to other functions called from within that function. It’s a way of coding the intention that “at coding time, we don’t know what additional arguments or how many may be passed in, so we’re just going to accept any number of additional arguments.” You can choose to parse them yourself and make use of them or simply pass them on to other functions you invoke within your function.

?`..1`
.x and .y Often used as a generic variable name for any type of object: see purrr::map and purrr::map2.
.f Often used as a generic variable name for a function object: see purrr::map.
.data Often used as a generic variable name for a data.frame object, especially when being used as the first parameter to a function written to play nicely in the tidyverse: see dplyr::select.
df This could mean “degrees of freedom”, but it’s also often used as a mnemonic generic variable name for a data.frame object, when you don’t yet know a better, more descriptive name to use. Consider it the “this could be any data.frame” name for a data.frame. It’s often found in early iterations of exploratory code and in example code. You should use more intention revealing variable names, when you can.
.Last.value This is the variable name R automatically associates with the most recently evaluated object. Usually hidden, RStudio has an option to make it visible in your Environment pane: Tools > Global options… > General > Advanced > Show .Last.value in environment listing
`` The backtick character is found to the left of the 1 key on U.S. English keyboards and is used to delimit names of objects that would otherwise not be allowed:

 

?`%in%`

Examples (contrived and otherwise)

.

library(magrittr)
letters %>% grepl("[aeiou]", .)

library(dplyr)
mtcars %>% mutate(design = paste(.$cyl, .$carb, .$am, sep = "x"))

example <- function(a, b, ...) {
  sum(a, b, ...)
}

example(40, 2)  # 42
example(40, NA) # NA
example(40, NA, na.rm = TRUE) # 40

.x and .y

example <- function(.x, .y) {
  sum(.x, .y)
}

.f

example <- function(.x, .y, .f) {
  .f(.x, .y)
}

example(40, 2, sum)
example(40, 2, `*`)

.data

library(dplyr)

example <- function(.data) {
  .data %>%
    slice(1:3) %>%
    select(2, 4, 6)
}

example(mtcars)

df

df <- mtcars

head(df)

You can think of the df example as saying, "however you get your data–whether from a CSV file, from the web, or by hand creating it–you generate a data.frame. Now, here's what you can do with that data.frame."

.Last.value

head(mtcars)
.Last.value$cyl

tail(mtcars)
x <- .Last.value
str(x)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.