Remove quotes from a character vector in R

前端 未结 10 2190
盖世英雄少女心
盖世英雄少女心 2020-11-29 01:06

Suppose you have a character vector:

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

When you make reference to an index value, you get the follo

相关标签:
10条回答
  • 2020-11-29 01:28

    If:

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

    You can:

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

    Your result should be:

    [1] one

    0 讨论(0)
  • 2020-11-29 01:31

    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")
    }
    
    0 讨论(0)
  • 2020-11-29 01:32

    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.

    0 讨论(0)
  • 2020-11-29 01:33

    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
    
    0 讨论(0)
  • 2020-11-29 01:33

    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.

    0 讨论(0)
  • 2020-11-29 01:37

    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 
    
    0 讨论(0)
提交回复
热议问题