65

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
Milktrader
  • 9,278
  • 12
  • 51
  • 69
  • So you just want to remove quotes when printing it to standard output (or to a file) ? – Prasad Chalasani Mar 07 '11 at 03:32
  • 2
    I'm trying to loop elements of a character vector through a function, but I need the quotes off. – Milktrader Mar 07 '11 at 03:34
  • 1
    I guess I still don't understand the exact purpose -- perhaps posting the use-example would help – Prasad Chalasani Mar 07 '11 at 03:35
  • 2
    `cat("[1]", char[1], "\n")`? Just kidding... I don't quite get what are you up to... – aL3xa Mar 07 '11 at 03:36
  • I just found noquote() by searching ??strip. @Prasad, 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, and it is that xts object that I want to pass through a function. – Milktrader Mar 07 '11 at 03:38
  • 1
    I don't know that I understand your use case either, but I think you probably want as.symbol() or as.name() instead. If this is from a package, your function likely wants a symbol, not a noquote, though hard to say what will work. – Noah Mar 07 '11 at 03:46
  • Example usage: copy/paste/edit the output of `dput(names(data_frame))` into a `dplyr::select` function call – ggll Oct 16 '18 at 23:51

11 Answers11

129

Just try noquote(a)

noquote("a")

[1] a

AlexArgus
  • 1,429
  • 2
  • 9
  • 3
  • Great answer! I've been using R for 9 years and I'm still learning cool new functions all the time. – Andrew Brēza Jun 01 '21 at 13:51
  • Hi @AlexArgus, is there a way to carry this on to once you have saved the file out of R? Once I ty to open it out of R / on the command line, the quotes are there? Thanks! – ayeepi Apr 25 '22 at 17:24
34

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.

Allan Engelhardt
  • 1,421
  • 10
  • 5
31

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"
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Noah
  • 2,574
  • 1
  • 18
  • 12
  • 8
    as.name() and as.symbol() both convert the char[1] element to the "name" class. noquote() converts the char[1] element to the "noquote" class. Hmmm, I didn't know R had so many classes. Thanks. – Milktrader Mar 07 '11 at 03:54
  • a simpler solution is given by @AlexArgus as noquote("a") – Rotail Jul 27 '17 at 17:27
  • `as.name`, `as.symbol` and `get` all only allow names less than 10000 bytes. `noquote` does not have that limit. – Chintan Pathak Nov 07 '18 at 23:09
  • Hmmm, does anyone knows how to get a _character_ class after `noquote` but _without_ quotes? – abc Feb 08 '21 at 08:44
  • how to get the same result as as.name(), but with a vector of values? – merry123 Nov 19 '22 at 15:04
21

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
Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • 1
    thanks for reading the comments, where the real question was embedded. This is exactly what I want to do - get objects out of the environment by name. I wrongly thought it was simply a matter dropping quotes, but that didn't work. Now I have get(). – Milktrader Mar 07 '11 at 13:16
  • 1
    +1 , There is lot to learn from what you have stated, Thanks. – PKumar Sep 03 '14 at 13:43
6

If:

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

You can:

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

Your result should be:

[1] one

Alex Poma
  • 69
  • 1
  • 4
6

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
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
4

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.

irritable_phd_syndrome
  • 4,631
  • 3
  • 32
  • 60
3

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”
NelsonGon
  • 13,015
  • 7
  • 27
  • 57
3

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")
}
taskinoor
  • 45,586
  • 12
  • 116
  • 142
Randall Goodwin
  • 1,916
  • 2
  • 18
  • 34
3

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

> cat(noquote("love"))
love

else just use noquote

> noquote("love")
[1] love
vivek
  • 301
  • 3
  • 13
1

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 
ForceMagic
  • 6,230
  • 12
  • 66
  • 88
schenken
  • 19
  • 2
  • 1
    Welcome on SO. No need to include the wrong answer in your answer, you should only put the correct one. I also suggest you to have a look to our Tour and Help section to learn more about posting answer on SO. http://stackoverflow.com/tour http://stackoverflow.com/help Good Luck. – ForceMagic Mar 06 '14 at 15:05