R encoding ASCII backtick

2019-07-18 18:52发布

问题:

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!

回答1:

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