[R] Fuction Error

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Thu Dec 6 22:17:38 CET 2012


R doesn't form opinions about your data. If you are not paying attention to what the functions you call expect to receive, then you may find your experience with R is frustrating.

In this case, we have no idea what the function "symbolFrame" is, or what it expects to receive (and it did not come up when I used RSiteSearch).

It is common in pattern matching tasks to use regular expressions. A carat symbol at the beginning of a pattern has special meaning. If sumbolFrame is something you can edit, one solution might be to change the functions that symbolFrame calls to not use regular expressions. Alternately, you can learn RE syntax and escape the carat symbol so it is not interpreted specially.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

Douglas Karabasz <douglas at sigmamonster.com> wrote:

>I'm calling a list of symbols and then using a function to build a data
>frame from that symbol list.  It works great until I introduce this
>index
>symbol from yahoo '^GSPC'.  When and index symbol is introduced I get
>and
>error which is below.
>
>> Data <- symbolFrame(symbols)  
>
>Error in get(S) : object '^GSPC' not found
>
> 
>
>Since R does not like the ^ in front of a name it would seem that my
>function needs away around the ^ in the symbol list.  When I call this
>function below it would be fine if you drop the ^.  So R just calls the
>data
>fine and drops that one "^".  
>
> 
>
> 
>
>> head(GSPC)
>      GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume GSPC.Adjusted
>2007-01-03   1418.03   1429.42  1407.86    1416.60  3429160000      
>1416.60
>2007-01-04   1416.60   1421.84  1408.43    1418.34  3004460000      
>1418.34
>2007-01-05   1418.34   1418.34  1405.75    1409.71  2919400000      
>1409.71
>2007-01-08   1409.26   1414.98  1403.97    1412.84  2763340000      
>1412.84
>2007-01-09   1412.84   1415.61  1405.42    1412.11  3038380000      
>1412.11
>2007-01-10   1408.70   1415.99  1405.32    1414.85  2764660000      
>1414.85
> 
>This produces the data just fine. However, if I call it like this.
> 
>> head(^GSPC)
>Error: unexpected '^' in "head(^"
> 
>Again it is a problem.
> 
>Back to the real problem here is the code below.  How do I get around
>the
>error that results from the "^"?  
> 
>rm(list = ls(all = TRUE)) # use this to clear data
>library(quantmod)
>library(PerformanceAnalytics)
> 
>symbols <-
>c('XLE','XLV','XLI','XLU','XLP','IYZ','XLK','XLY','XLF','XLB','GLD','SLV','E
>FA','EEM','FXA','FXE','FXY','HYG','LQD', '^GSPC')
> 
>getSymbols(symbols,from='2007-01-01')
>getSymbols('SPY',from='2007-01-01')
> 
>SP500 <- Cl(SPY)
>colnames(SP500)[1] <- 'SPY'
> 
>#Function to build a dataframe form a list of symbols
>symbolFrame <- function(symbolList) {
>  Data <- data.frame(NULL)
>  for (S in symbolList) {
>    Data <- cbind(Data,Cl(get(S)))
>  }
>  colnames(Data) <- symbolList
>  return(Data)
>  
>}
> 
>Data <- symbolFrame(symbols)  
> 
> 
>
> 
>
> 
>
> 
>
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list