user8491385
user8491385

Reputation: 453

Replace Double (") to Single Straight line Quote (') in R

I have the following IDs and want to replace the double quote (") with a single straight line quote (').

ID<-c("1WK7X6", "TQM6VY", "ZDNZX0", "J495D8", "RGNSW", 
"9ZD31", "9JS84", "NTHDJ4", "H2UA1", "AV9N7", 
"DC5F2B6", "SLL2C2", "MB2Q0", "9C94LR", "KZNFS0", 
"WHBH47", "BHPW9", "CH2TD5", "TN8F", "XHCQ41"

Upvotes: 0

Views: 909

Answers (2)

Pramesh Pudasaini
Pramesh Pudasaini

Reputation: 113

I was running into a similar issue of converting double-quoted character (or quote-less numeric) vector into single-quoted vector separated by commas and surrounded by parentheses so that this new vector could be passed to a SQL query.

ID <- c("1WK7X6", "TQM6VY", "ZDNZX0", "J495D8", "RGNSW", 
      "9ZD31", "9JS84", "NTHDJ4", "H2UA1", "AV9N7", 
      "DC5F2B6", "SLL2C2", "MB2Q0", "9C94LR", "KZNFS0", 
      "WHBH47", "BHPW9", "CH2TD5", "TN8F", "XHCQ41")

new_ID <- paste(sQuote(ID, "'"), collapse = ",")
new_ID <- paste0("(", new_ID, ")")
new_ID

This works for numeric vectors as well.

num <- c(1:10)

new_num <- paste(sQuote(num, "'"), collapse = ",")
new_num <- paste0("(", new_num, ")")
new_num

Upvotes: 0

Huanfa Chen
Huanfa Chen

Reputation: 617

Can you print the result of class(ID)? If it is a vector of characters, then the double quotes are irrelevant as it is only used to show that each element is a character.

Upvotes: 1

Related Questions