Dennis
Dennis

Reputation: 45

parse string with numbers or hyphens as one string in R

I am trying to parse strings with hyphens and/or numbers to call specific rows.

gene_name <- c("EP-CAM")
Genename=paste0("RNA$",gene_name)
Gene=eval(parse(text = paste0(Genename)))

This is the error:

Error in eval(parse(text = paste0(Genename))) : 
  object 'CAM' not found

I would need to get RNA$EP-CAM parsed for example. Backquotes will not give me the output and only show me the string.

With numbers the same would happen. I guess this is just a problem of the parse command. Is there an alternative to it? This is in analogy to this problem: Unexpected symbol error in parse(text = str) with hyphen after a digit

Thank you so much for you support.

D

Upvotes: 0

Views: 607

Answers (1)

dayne
dayne

Reputation: 7784

Adding back ticks to the call works for me. The problem, here, is that "EP-CAM" isn't really a valid name.

RNA <- list(`EP-CAM` = 0)
gene <- c("EP-CAM")
geneName <- paste0("RNA$`", gene, "`")
eval(parse(text = geneName))
# [1] 0

In fact, the following renames the column as EP.CAM.

data.frame(`EP-CAM` = 0)
#   EP.CAM
# 1      0

Upvotes: 2

Related Questions