0

I have some characters stored as cols. The resulting output is as below:

cols = c("big creek", "gage creek", "garvey creek", "larches creek", "little usable")
cols
[1] "big creek"       "gage creek"      "garvey creek"    "larches creek"   "little usable"

However, I want the quotes to be replaced with backticks and the resulting output should be like this:

[1] `big creek`       `gage creek`      `garvey creek`    `larches creek`   `little usable`

Is there any way to get the same output for cols object in R?

Your help will be highly appreciated.

Regards, Farhan

Farhan
  • 57
  • 5

3 Answers3

2

You cannot have a vector that contain backtick quoted names. Since you said you are using for loop, you can transform each name to contain the backticks within the for loop using as.name:

as.name(cols[1])
`big creek`

lapply(cols, as.name)
[[1]]
`big creek`

[[2]]
`gage creek`

[[3]]
`garvey creek`

[[4]]
`larches creek`

[[5]]
`little usable`
Onyambu
  • 67,392
  • 3
  • 24
  • 53
  • Thank you. This is what I was looking for to get the output but using this for `y` argument in aesthetics for ggplot it considered cols[1] as symbol not as data column. However pasting the output of as.names(cols[1]) really worked to print the graph. I have modified the `data` argument and set filter for each site and it worked well in loop function. Thanks all you of you for your valuable comments. – Farhan Feb 27 '22 at 12:08
0

Your strings don't actually have quotes, they just appear that way on the console. Probably it bothers you that you have to set backticks around variable names with spaces, when subsetting with $.

In R, syntactically valid object names should consist of letters, numbers, dots, and underscores only, no numbers or dots as the first character. You could easily fix that using make.names.

Example:

df <- data.frame(`big creek`=1:3, `gage creek`=4:6, `garvey creek`=7:9, check.names=F)
df
#   big creek gage creek garvey creek
# 1         1          4            7
# 2         2          5            8
# 3         3          6            9

names(df) 
# [1] "big creek"    "gage creek"   "garvey creek"  ## invalid names

df$`big creek`  ## rather annoying backticks needed
# [1] 1 2 3

cols1 <- names(df)  ## vector of column names
cols1
# [1] "big creek"    "gage creek"   "garvey creek"

make.names(cols1)  ## `make.names` fixes names
# [1] "big.creek"    "gage.creek"   "garvey.creek"

names(df) <- make.names(cols1)
names(df) 
# [1] "big.creek"    "gage.creek"   "garvey.creek"  ## valid names

## actually you don't need to cache in a vector and just may do
names(df) <- make.names(names(df))

From then on, use valid names for coding and label your data separately, e.g.:

barplot(colSums(df), names.arg=c("big creek", "gage creek", "garvey creek"))

enter image description here

That said, if you still want to have your strings surrounded with backticks, you can use sprintf.

sprintf('`%s`', cols)
# [1] "`big creek`"     "`gage creek`"    "`garvey creek`"  "`larches creek`" "`little usable`"
jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • Thank you for your quick response. I was actually working on plotting graphs in ggplot2 package with for loop function to print multiple plots in a grid. For `aes(x = datetime, y = "big creek")` the quotes or character nature of y argument is not acceptable. When I replace "" with bacticks the code works well. But for loop I wants the y argument as `y = cols[i]` and here cols[i] should print `big creek` instead of "big creek". – Farhan Feb 27 '22 at 06:21
  • @Farhan Use `make.names` as shown to fix the invalid column names. Differentiate between how objects are named in R, and how something appears on an output like a plot. – jay.sf Feb 27 '22 at 06:26
  • I tried to use `make.names` for cols object. Then I used cols[1] for y argument in aes() function but it resulted in error: Error: Discrete value supplied to continuous scale – Farhan Feb 27 '22 at 06:33
  • @Farhan Perhaps I wasn't clear, please see my updated answer. – jay.sf Feb 27 '22 at 06:53
  • Thank you for the detailed answer. It is working in plot function but not as an answer to y argument of `aes()` in `ggplot()` function. I am trying to answer the y argument as cols[1] to print the plot for "big greek" but I don't want to actually type the name manually for each site. I can use cols[i] in for loop to auto use these column names but problem is there when i use cols[1] as an answer to y argument it prints "big greek" with quotes and not the backticks (without quotes). – Farhan Feb 27 '22 at 07:17
  • @Farhan No clue what you're doing, make a [reproducible example](https://stackoverflow.com/a/5963610/6574038). – jay.sf Feb 27 '22 at 07:21
0

Maybe something like this:

result <- gsub('^(.{0})(.*)$', '\\1`\\2`', cols)
paste(result, collapse = " ")

output:

1] "`big creek` `gage creek` `garvey creek` `larches creek` `little usable`"
TarJae
  • 72,363
  • 6
  • 19
  • 66