Remove quotes from a character vector in R

198,408

Solution 1

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"

Solution 2

Just try noquote(a)

noquote("a")

[1] a

Solution 3

There are no quotes in the return value, only in the default output from print() when you display the value. Try

> print(char[1], quote=FALSE)
[1] one

or

> cat(char[1], "\n")
one

to see the value without quotes.

Solution 4

You are confusing quantmod's 'symbol' (a term relating to a code for some financial thingamuwot) with R's 'symbol', which is a 'type' in R.

You've said:

I have a character vector of stock symbols that I pass to quantmod::getSymbols() and the function returns the symbol to the environment without the quotes

Well almost. What it does is create objects with those names in the specified environment. What I think you want to do is to get things out of an environment by name. And for that you need 'get'. Here's how, example code, working in the default environment:

getSymbols('F',src='yahoo',return.class='ts') [1] "F"

so you have a vector of characters of the things you want:

> z="F"
> z
[1] "F"

and then the magic:

> summary(get(z))
     F.Open           F.High           F.Low           F.Close      
 Min.   : 1.310   Min.   : 1.550   Min.   : 1.010   Min.   : 1.260  
 1st Qu.: 5.895   1st Qu.: 6.020   1st Qu.: 5.705   1st Qu.: 5.885  
 Median : 7.950   Median : 8.030   Median : 7.800   Median : 7.920  
 Mean   : 8.358   Mean   : 8.495   Mean   : 8.178   Mean   : 8.332  
 3rd Qu.:11.210   3rd Qu.:11.400   3rd Qu.:11.000   3rd Qu.:11.180  
 Max.   :18.810   Max.   :18.970   Max.   :18.610   Max.   :18.790  

and if you don't believe me:

> identical(F,get(z))
[1] TRUE

Solution 5

If:

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

You can:

> print(char[1],quote = FALSE)

Your result should be:

[1] one

Share:
198,408
Milktrader
Author by

Milktrader

Updated on July 08, 2022

Comments

  • Milktrader
    Milktrader almost 2 years

    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