When are Backticks ' ' used compared to Double Quotes " "?

Viewed 659

What I know so far ...

1) Backticks are used when creating tibbles with non-syntactic variable/column names that contain numbers, spaces, or other symbols (because normally you can only name columns with letters right?)

 tb <- tibble( 
               ': ) ' = "smile, ' ' = "space", 
               '2000' = "number", "double_quotes" = "normal_text")

However, when I use double quotes here the tibble still forms with the nonsyntactic symbols/numbers.

2) Double quotes are used to subset column names when using double brackets.

  tb[["double_quotes"]]

And here, when I use single quotes to subset, it still works as well.

3) When subsetting using $, to select for nonsyntactic names, I must use single quotes, but here again, if I subset using double quotes, it works as well

Again, tb$": )" works just as well as tb$': )'

So are they effectively interchangeable?


Interestingly, when I plot a graph

annoying <- tibble(
  `1` = 1:10,
  `2` = `1` * 2 + rnorm(length(`1`))
)

ggplot(annoying, aes(x = `1`, y = `2`)) +
  geom_point()

Single quotes must be used when referring to the nonsyntactic variables because otherwise, it looks like ggplot treats X and Y as single points of 1 and 2 respectively. Are there any other cases like this?

1 Answers

It's important to distinguish between single quotes (') and backticks (or "back-single-quotes") (`).

Most of what you want to know is in ?Quotes:

Single (') and double (") quotes delimit character constants. They can be used interchangeably but double quotes are preferred (and character constants are printed using double quotes), so single quotes are normally only used to delimit character constants containing double quotes.

Almost always, other [i.e., non-syntactically valid] names can be used provided they are quoted. The preferred quote is the backtick (‘`’) ... under many circumstances single or double quotes can be used (as a character constant will often be converted to a name). One place where backticks may be essential is to delimit variable names in formulae: see ‘formula’.

For example, if you want to define a variable name containing a space, you need back-ticks:

`a b` <- 1

Double quotes also work here (to my surprise!)

"a b" <- 1

but if you want to use the resulting variable in an expression you'll need to use back-ticks. "a b" + 1 gives an error (" non-numeric argument to binary operator") but `a b`+1 works.

As @r2evans points out, the same rules apply in tidyverse expressions. You can use double- or single-quotes (if you want) to define new variables: mtcars %>% mutate("my stuff"=4), but if you want to subsequently use that variable (or any other non-syntactic variable) in an expression, you have to backtick-protect it: mtcars %>% mutate("my stuff"=4, new=`my stuff` + 5).

It's probably best practice/least confusing to just use backticks for all non-syntactic variable reference and single quotes for character constants.

Related