2

I have the following backtick on my list's names. Prior lists did not have this backtick.

$`1KG_1_14106394`
[1] "PRDM2"

$`1KG_20_16729654`
[1] "OTOR"

I found out that this is a 'ASCII grave accent' and read the R page on encoding types. However what to do about it ? I am not clear if this will effect some functions (such as matching on list names) or is it OK leave it as is ?

Encoding help page: https://stat.ethz.ch/R-manual/R-devel/library/base/html/Encoding.html

Thanks!

avari
  • 105
  • 5

1 Answers1

3

My understanding (and I could be wrong) is that the backticks are just a means of escaping a list name which otherwise could not be used if left unescaped. One example of using backticks to refer to a list name is the case of a name containing spaces:

lst <- list(1, 2, 3)
names(lst) <- c("one", "after one", "two")

If you wanted to refer to the list element containing the number two, you could do this using:

lst[["after one"]]

But if you want to use the dollar sign notation you will need to use backticks:

lst$`after one`

Update:

I just poked around on SO and found this post which discusses a similar question as yours. Backticks in variable names are necessary whenever a variable name would be forbidden otherwise. Spaces is one example, but so is using a reserved keyword as a variable name.

if <- 3     # forbidden because if is a keyword
`if` <- 3   # allowed, because we use backticks

In your case:

Your list has an element whose name begins with a number. The rules for variable names in R is pretty lax, but they cannot begin with a number, hence:

1KG_1_14106394   <- 3  # fails, variable name starts with a number
KG_1_14106394    <- 3  # allowed, starts with a letter
`1KG_1_14106394` <- 3  # also allowed, since escaped in backticks
Community
  • 1
  • 1
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • That makes sense. Do you think the underscore might be the problem then ? Some of the list names also have a '. ' as in `$variant.111` but that is it special character wise ? – avari Jul 13 '16 at 13:43
  • @avari I updated my answer. The underscores are fine, the problem is the variable name beginning with a number (which is disallowed in many programming languages). – Tim Biegeleisen Jul 13 '16 at 13:51
  • Ah ha! Thanks again. I would prefer to leave the list names are they are (not my choice unfortunetly), but they should be fine to use with functions such as 'match' since they are escaped correct ? – avari Jul 13 '16 at 14:00
  • I think it really depends on the scenario. In my example above, when accessing a list as `lst[["after one"]]`, no explicit escaping is needed because the double quotes effectively escape _all_ names. When using the dollar sign operator, escaping via backticks certainly is needed in your case. – Tim Biegeleisen Jul 13 '16 at 14:04