[英]How do I properly call/subset a specific column in a xts object?
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).我通过 quantmod 导入了一些时间序列数据,并且相应的 xts 对象包含几个不同的列,而不仅仅是随着时间的推移价格不同的列(打开关闭等)。 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?我如何正确调用/子集 xts 对象中的特定列,因为我只需要使用其中一个进行进一步分析? getSymbols gets me several price infos per day I dont all need/want. getSymbols 每天为我提供几个我并不需要/想要的价格信息。
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.当然我想保留 xts 结构。 Thank you a lot!非常感谢!
The object is created in the environment.对象是在环境中创建的。 We can check ls()
我们可以检查ls()
ls()
#[1] "data" "GOOGL"
If we check data
, it would be just the character string如果我们检查data
,它将只是字符串
data
#[1] "GOOGL"
and that is the reason it is not working这就是它不起作用的原因
The object name would be the same symbol name used in getSymbols
.对象名称与getSymbols
使用的符号名称相同。 So, use所以,使用
GOOGL[, "GOOGL.Open"]
Or we can get
the value with或者我们可以get
与价值
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
.选项auto.assign = FALSE
因为它默认为TRUE
。 On a fresh R
session,在新的R
会话中,
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.由于auto.assign
为 FALSE,因此它不会创建 GOOGL 对象。
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
After you run getSymbols
, the data is present in GOOGL
and not data
.运行getSymbols
,数据出现在GOOGL
而不是data
。 Try尝试
GOOGL[, "GOOGL.Open"]
GOOGL[, 3]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.