How to access xts column by name in a function in R?

91 Views Asked by At

How would I access an xts column by name in a function? The code below returns NULL.

#m(list=ls())
#dev.off(dev.list()["RStudioGD"])
getSymbols("^GSPC",src="yahoo",from="2015-01-01",to = "2020-06-04")
str(GSPC)
frog<-function(ts,name) {
    ts$name
}
frog(GSPC,"GSPC.Close")
3

There are 3 best solutions below

2
G. Grothendieck On BEST ANSWER

quantmod provides Cl and Ad functions to extract the close and adjusted close. See ?Cl for these and other extraction functions. For example, here are ways to get the close. Note that in the second one the portion after the $ must be the column name and not a variable holding the column name. Lists and data.frames work the same way with $ in R.

Cl(GSPC)
GSPC$GSPC.Close
GSPC[, "GSPC.Close"]
col <- "GSPC.Close"; GSPC[, col]
GSPC[, 4] # close is 4th column

frog <- function(x, col) if (is.function(col)) col(x) else x[, col]
frog(GSPC, Cl)
frog(GSPC, "GSPC.Close")
frog(GSPC, 4)
0
MrFlick On

You can't use $ with string variables. For objects of type xts, you need to subset with the [<row>,<col>] syntax. So you need to do

frog <- function(ts,name) {
  ts[, name]
}
frog(GSPC,"GSPC.Close")
1
IRTFM On

If you're going to use pkg:quantmod, then you need to understand "xts"-classed objects which in turn inherit from the "zoo"-class when there is no xts-specific function defined:

#Your first lien of code should have been
> library(quantmod)

> class(GSPC)
[1] "xts" "zoo"

The data portion of such objects are accessed with matrix-like functions although they need to be specifically built in the package code:

> inherits(GSPC, "matrix")
[1] FALSE

# so not really a matrix but it has a matrix built into it:

> str(GSPC)
An xts object on 2015-01-02 / 2020-06-03 containing: 
  Data:    double [1364, 6]
  Columns: GSPC.Open, GSPC.High, GSPC.Low, GSPC.Close, GSPC.Volume ... with 1 more column
  Index:   Date [1364] (TZ: "UTC")
  xts Attributes:
    $ src    : chr "yahoo"
    $ updated: POSIXct[1:1], format: "2023-06-30 13:50:33"

If we remove its class labels it, like all zoo objects, is a matrix at its core.

str(unclass(GSPC))
 num [1:1364, 1:6] 2059 2054 2022 2006 2031 ...
 - attr(*, "index")= num [1:1364] 1.42e+09 1.42e+09 1.42e+09 1.42e+09 1.42e+09 ...
  ..- attr(*, "tzone")= chr "UTC"
  ..- attr(*, "tclass")= chr "Date"
 - attr(*, "src")= chr "yahoo"
 - attr(*, "updated")= POSIXct[1:1], format: "2023-06-30 13:52:43"
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:6] "GSPC.Open" "GSPC.High" "GSPC.Low" "GSPC.Close" ...

> inherits(unclass(GSPC), "matrix")
[1] TRUE

GSPC[ , "GSPC.Close"] should succeed

> GSPC[ , "GSPC.Close"]
           GSPC.Close
2015-01-02    2058.20
2015-01-05    2020.58
2015-01-06    2002.61
2015-01-07    2025.90
2015-01-08    2062.14
2015-01-09    2044.81
2015-01-12    2028.26
2015-01-13    2023.03
2015-01-14    2011.27
2015-01-15    1992.67
       ...           
2020-05-20    2971.61
2020-05-21    2948.51
2020-05-22    2955.45
2020-05-26    2991.77
2020-05-27    3036.13
2020-05-28    3029.73
2020-05-29    3044.31
2020-06-01    3055.73
2020-06-02    3080.82
2020-06-03    3122.87
> 

Note that the print.xts function returns console output in a manner that resembles that of print.data.table