0

As the title suggests, I keep getting back an error message when trying to create a string of characters in R.

Here is some background into my problem.

I am working on a project where I'll be modifying and testing the Markowitz portfolio selection method by using the 503 stocks currently listed as a part of the S&P 500. I've sourced all 503 tickers with their daily Open,Close, Adjusted, High, Low, and Volume data with quantmod and merged them into an xts object called Full.

tickers = c("AAPL", "MSFT", "AMZN",...)
quantmod(getSymbols(tickers, src = 'yahoo', from = Sys.Date()-(20*365))
Full = merge(SPY,AAPL, MSFT, AMZN, ...)

Now I want to remove the High, Low, and Volume data from this xts object by creating a subset. Ideally, the code should look something like this.

subset1 = c("SPY.High","SPY.Low","SPY.Volume",...)
Full.OCAV = subset(FULL, select = !subset1)

However, the first line of this code either spits back an unexpected symbol error or prompts me to close the code by placing me in o + line on the console (this happens even when I do not edit the code).

I guess it is possible that there is a typo in the string, however there are two reasons I am so confident there is not. Firstly, I created the list of string characters in excel with the concatenate function, so I know the syntax of each element is identical. I did the same thing to create the 'tickers' vector which worked without any issues. Second, subset1=... works if I remove some of the character elements. I even removed all but the last 20 elements and got it to work, so I checked the last 20 elements for typos and there are none.

I guess I could create two subsets and merge them, but I'd like to have this be tractable because I'll need to perform other mass operations on the dataset going forward and I don't want to have to create many small subsets each time.

Is this an issue of the size of the string? There are 503*3 elements and I am running R on my laptop.

  • 1
    Do you have all these values on a single line? R has a maximum line length that the interpreter can handle. It's around 4000 characters. You can split up the values on multiple lines. See https://stackoverflow.com/questions/54974996/how-to-bypass-rstudio-console-upper-limit-on-character-string-length for a similar issue. – MrFlick Apr 25 '23 at 15:05
  • 1
    Also, there are probably more efficient ways to drop those columns using a regular expression match. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Apr 25 '23 at 15:09
  • @MrFlick, just took your first recommendation and I got the code to work, thank you! I'll look into more efficient ways of removing columns, too/ – seanmag Apr 25 '23 at 18:38

0 Answers0