Know very little about R. Trying to adapt some codes to finish an assignment. However, got stuck with the dash in the ticker.
symbol.vec=c("BTC-USD","ETH-USD")
getSymbols(symbol.vec,from="2015-08-07",to="2017-12-16")
BTC = BTC-USD[,"BTC-USD.Adjusted",drop=F]
Error: object 'USD' not found
Apparently, the dash is taken as a minus. How can I change the names of the data frame so that I can reference them?
Dashes are not legal in R object or column names. To avoid an error, you need to enclose the illegal name in backticks. Your code will work if you do the following:
BTC = `BTC-USD`[ , "BTC-USD.Adjusted", drop=F]
Quotation marks will work for indexing inside braces, so only the reference to the object needs to be enclosed in backticks. However, to use the dollar-sign notation, you'd need backticks for the column name too, giving this abominable bit of code:
`BTC-USD`$`BTC-USD.Adjusted`
It's normally better to work with legal column names and only change to "real-world" names when outputting to graphs, tables, etc.
As a new R user, the most intuitive and transparent approach for changing object and column names is probably to just rename the data explicitly for each symbol. We'll switch from hyphens to underscores, since underscores are legal:
# Change name of object to legal name
BTC_USD = `BTC-USD`
# Change column names to legal names
names(BTC_USD) = gsub("-", "_", names(BTC_USD))
A more efficient, but less intuitive and transparent approach is to put the data in a list and operate on the list:
# Put data into a list; one symbol per list element
dat = mget(symbol.vec)
names(dat)
lapply(dat, head)
> names(dat) [1] "BTC-USD" "ETH-USD" > lapply(dat, head) $`BTC-USD` BTC-USD.Open BTC-USD.High BTC-USD.Low BTC-USD.Close BTC-USD.Volume BTC-USD.Adjusted 2015-08-06 278.00 279.60 274.28 277.89 11919665 277.89 2015-08-07 277.89 278.92 257.42 258.60 22308123 258.60 2015-08-08 258.60 266.75 258.56 263.87 15154749 263.87 2015-08-09 263.87 266.63 260.52 263.30 12873441 263.30 2015-08-10 263.30 269.90 261.44 269.03 13681939 269.03 2015-08-11 269.03 271.50 263.66 267.66 15232934 267.66 $`ETH-USD` ETH-USD.Open ETH-USD.High ETH-USD.Low ETH-USD.Close ETH-USD.Volume ETH-USD.Adjusted 2015-08-06 0.6747 3.00 0.6747 3.00 371 3.00 2015-08-07 3.0000 3.00 0.1500 1.20 1438 1.20 2015-08-08 1.2000 1.20 1.2000 1.20 0 1.20 2015-08-09 1.2000 1.20 1.2000 1.20 0 1.20 2015-08-10 1.2000 1.20 0.6504 0.99 7419 0.99 2015-08-11 0.9900 1.29 0.9050 1.29 2376 1.29
# Rename list elements and columns to legal names
names(dat) = gsub("-", "_", names(dat))
dat = lapply(dat, function(x) setNames(x, gsub("-","_", names(x))))
names(dat)
lapply(dat, head)
> names(dat) [1] "BTC_USD" "ETH_USD" > lapply(dat, head) $BTC_USD BTC_USD.Open BTC_USD.High BTC_USD.Low BTC_USD.Close BTC_USD.Volume BTC_USD.Adjusted 2015-08-06 278.00 279.60 274.28 277.89 11919665 277.89 2015-08-07 277.89 278.92 257.42 258.60 22308123 258.60 2015-08-08 258.60 266.75 258.56 263.87 15154749 263.87 2015-08-09 263.87 266.63 260.52 263.30 12873441 263.30 2015-08-10 263.30 269.90 261.44 269.03 13681939 269.03 2015-08-11 269.03 271.50 263.66 267.66 15232934 267.66 $ETH_USD ETH_USD.Open ETH_USD.High ETH_USD.Low ETH_USD.Close ETH_USD.Volume ETH_USD.Adjusted 2015-08-06 0.6747 3.00 0.6747 3.00 371 3.00 2015-08-07 3.0000 3.00 0.1500 1.20 1438 1.20 2015-08-08 1.2000 1.20 1.2000 1.20 0 1.20 2015-08-09 1.2000 1.20 1.2000 1.20 0 1.20 2015-08-10 1.2000 1.20 0.6504 0.99 7419 0.99 2015-08-11 0.9900 1.29 0.9050 1.29 2376 1.29