Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove quotes from a character vector in R

Tags:

r

quotes

Suppose you have a character vector:

char <- c("one", "two", "three") 

When you make reference to an index value, you get the following:

> char[1] [1] "one" 

How can you strip off the quote marks from the return value to get the following?

[1] one 
like image 905
Milktrader Avatar asked Mar 07 '11 03:03

Milktrader


People also ask

How do you remove a quote from a variable?

A single line sed command can remove quotes from start and end of the string. The above sed command execute two expressions against the variable value. The first expression 's/^"//' will remove the starting quote from the string. Second expression 's/"$//' will remove the ending quote from the string.

How do you replace double quotes in R?

Just use "\"" or '"' to match a single double quote.

How do you remove quotation marks from a string?

To remove double quotes just from the beginning and end of the String, we can use a more specific regular expression: String result = input. replaceAll("^\"|\"$", ""); After executing this example, occurrences of double quotes at the beginning or at end of the String will be replaced by empty strings.


2 Answers

Just try noquote(a)

noquote("a")

[1] a

like image 89
AlexArgus Avatar answered Oct 17 '22 06:10

AlexArgus


as.name(char[1]) will work, although I'm not sure why you'd ever really want to do this -- the quotes won't get carried over in a paste for example:

> paste("I am counting to", char[1], char[2], char[3]) [1] "I am counting to one two three" 
like image 35
Noah Avatar answered Oct 17 '22 06:10

Noah