【问题标题】:to get data.frame from xts form data [closed]从 xts 表单数据中获取 data.frame [关闭]
【发布时间】:2012-07-27 09:17:14
【问题描述】:
>require(quantmod)   
>setSymbolLookup(SDB=list(name="000001.sz",src="yahoo"))   
>getSymbols("SDB",from="2010-01-01",to="2010-01-10") 
> SDB

           000001.SZ.Open 000001.SZ.High 000001.SZ.Low 000001.SZ.Close
2010-01-04          24.52          24.58         23.68           23.71
2010-01-05          23.75          23.90         22.75           23.30
2010-01-06          23.25          23.25         22.72           22.90
2010-01-07          22.90          23.05         22.40           22.65
2010-01-08          22.50          22.75         22.35           22.60

我想要两种输出:
1.

> SDB
      date           open           high           low           close
2010-01-04          24.52          24.58         23.68           23.71
2010-01-05          23.75          23.90         22.75           23.30
2010-01-06          23.25          23.25         22.72           22.90
2010-01-07          22.90          23.05         22.40           22.65
2010-01-08          22.50          22.75         22.35           22.60

2.

> SDB
      date           open           high           low           close
1 2010-01-04          24.52          24.58         23.68           23.71
2 2010-01-05          23.75          23.90         22.75           23.30
3 2010-01-06          23.25          23.25         22.72           22.90
4 2010-01-07          22.90          23.05         22.40           22.65
5 2010-01-08          22.50          22.75         22.35           22.60

我怎样才能得到它?

【问题讨论】:

  • 对我来说,除了一种带有行名而一种没有行名之外,您的两种输出看起来相同。我认为您需要更具体地说明您想要什么和预期用途,以便人们为您提供更完善的解决方案。

标签: r xts


【解决方案1】:

这里有一个你想要的破解。我认为您的问题始于 SDB 的存储方式。要查看对象的外观,请使用str。我不知道xts 对象是什么,但使用str 可以帮助我确定如何取出我想要的部分。

str(SDB)
> str(SDB)
An ‘xts’ object from 2010-01-04 to 2010-01-08 containing:
  Data: num [1:5, 1:6] 24.5 23.8 23.2 22.9 22.5 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:6] "000001.SZ.Open" "000001.SZ.High" "000001.SZ.Low" "000001.SZ.Close" ...
  Indexed by objects of class: [Date] TZ: 
  xts Attributes:  
List of 4
 $ tclass : chr [1:2] "POSIXct" "POSIXt"
 $ tzone  : chr ""
 $ src    : chr "yahoo"
 $ updated: POSIXct[1:1], format: "2012-07-29 08:33:57"

现在我们可以开始工作了:

#grab first 4 columns and make it into a dataframe
NEW <- data.frame(SDB[, 1:4])

#turn the rownames to a column
NEW <- data.frame(date=rownames(NEW), NEW)

#remove the junk and lowercases the first letter
colnames(NEW) <- sub('^(\\w?)', '\\L\\1', 
    gsub("X000001.SZ.", "", colnames(NEW)), perl=T)  

#make rownames integer
rownames(NEW)<- NULL

#the two ways you wanted it to look
NEW
print(NEW ,row.names = FALSE) 

【讨论】:

  • > names(SDB)=c("open","high","low","close","volume","adjusted") > newdf = data.frame(date=index (SDB),SDB,row.names=NULL) 为什么我不能将两行合并为一行?
  • 我已经运行了你的代码,没关系。
  • @PengPeng, xts 对象有一个index 而不是行名,所以你可以使用data.frame(date = index(SDB), data.frame(SDB), row.names=NULL) 来获得一个基本的data.frame。另请参阅this question
  • @TylerRinker,这将是我的方法:temp = data.frame(date = index(SDB), data.frame(SDB)[1:4], row.names=NULL); names(temp) = tolower(gsub("X000001.SZ.", "", names(temp)))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-30
  • 1970-01-01
  • 1970-01-01
  • 2012-04-11
  • 2021-11-01
  • 1970-01-01
相关资源
最近更新 更多