Remove quotes from a character vector in R

RQuotes

R Problem Overview


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

R Solutions


Solution 1 - R

Just try noquote(a)

> noquote("a")

[1] a

Solution 2 - R

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 3 - R

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 - R

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 - R

If:

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

You can:

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

Your result should be:

[1] one

Solution 6 - R

I'm just guessing, is this in the ball park of what you're trying to achieve?

> a <- "a"
> a
[1] "a" # quote yes
> as.factor(a)
[1] a #quote no

Solution 7 - R

Easiest way is :

> a = "some string"
> write(a, stdout())  # Can specify stderr() also.
some string

Gives you the option to print to stderr if you're doing some error handling printing.

Solution 8 - R

I think I was trying something very similar to the original poster. the get() worked for me, although the name inside the chart was not inherited. Here is the code that worked for me.

#install it if you dont have it
library(quantmod)

# a list of stock tickers
myStocks <- c("INTC", "AAPL", "GOOG", "LTD")

# get some stock prices from default service
getSymbols(myStocks)

# to pause in between plots
par(ask=TRUE)

# plot all symbols
for (i in 1:length(myStocks)) {
	chartSeries(get(myStocks[i]), subset="last 26 weeks")
}
	

Solution 9 - R

Here is one combining noquote and paste:

noquote(paste("Argument is of length zero",sQuote("!"),"and",dQuote("double")))


#[1] Argument is of length zero ‘!’ and “double”

Solution 10 - R

Try this: (even [1] will be removed)

> cat(noquote("love"))
love

else just use noquote

> noquote("love")
[1] love

Solution 11 - R

nump function :)

> nump <- function(x) print(formatC(x, format="fg", big.mark=","), quote=FALSE)

correct answer: > x <- 1234567890123456

> nump(x)

[1] 1,234,567,890,123,456 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMilktraderView Question on Stackoverflow
Solution 1 - RAlexArgusView Answer on Stackoverflow
Solution 2 - RNoahView Answer on Stackoverflow
Solution 3 - RAllan EngelhardtView Answer on Stackoverflow
Solution 4 - RSpacedmanView Answer on Stackoverflow
Solution 5 - RAlex PomaView Answer on Stackoverflow
Solution 6 - RRoman LuštrikView Answer on Stackoverflow
Solution 7 - Rirritable_phd_syndromeView Answer on Stackoverflow
Solution 8 - RRandall GoodwinView Answer on Stackoverflow
Solution 9 - RNelsonGonView Answer on Stackoverflow
Solution 10 - RvivekView Answer on Stackoverflow
Solution 11 - RschenkenView Answer on Stackoverflow