2
votes

I imported some time series data via quantmod and the respective xts object contains several different columns not only one with different prices over time (open close ect). How do I properly call/subset a specific column in a xts object as I need only to work with one of them of further analysis? getSymbols gets me several price infos per day I dont all need/want.

library(quantmod)
data <- getSymbols("GOOGL",src = "yahoo", from = "2000-01-01", to = "2015-01-01")
data <- data[ ,"GOOGL.Open"] #trying to subset like this
data <- data[ , 3] #or this 

Nothin worked yet. There must be some easy solution. Of course I want to preserve the xts structure. Thank you a lot!

2
Issue options(getSymbols.auto.assign = FALSE) . Then after running getSymbols use Op(data)G. Grothendieck

2 Answers

0
votes

After you run getSymbols, the data is present in GOOGL and not data. Try

GOOGL[, "GOOGL.Open"]
GOOGL[, 3]
0
votes

The object is created in the environment. We can check ls()

ls()
#[1] "data"  "GOOGL"

If we check data, it would be just the character string

data
#[1] "GOOGL"

and that is the reason it is not working

The object name would be the same symbol name used in getSymbols. So, use

GOOGL[, "GOOGL.Open"]   

Or we can get the value with

get(data)[, "GOOGL.Open"]  

Or if we need to directly assign and wants more control, use the. option auto.assign = FALSE as it is by default TRUE. On a fresh R session,

data <- getSymbols("GOOGL",src = "yahoo", from = "2000-01-01",
        to = "2015-01-01", auto.assign = FALSE)

ls()
#[1] "data"

as the auto.assign is FALSE, it is not creating the GOOGL object.

head(data[, "GOOGL.Open"])
#         GOOGL.Open
#2004-08-19   50.05005
#2004-08-20   50.55556
#2004-08-23   55.43043
#2004-08-24   55.67567
#2004-08-25   52.53253
#2004-08-26   52.52753