问题描述
我有一个数据点的 csv 文件(例如金融报价、实验记录等),我的数据有重复的时间戳.这是演示问题的代码:
I've a csv file of data points (e.g. financial ticks, experiment recordings, etc.), and my data has duplicate timestamps. Here is code demonstrating the problem:
library(zoo);library(xts)
csv="2011-11-01,50
2011-11-02,49
2011-11-02,48
2011-11-03,47
2011-11-03,46
2011-11-03,45
2011-11-04,44
2011-11-04,43
2011-11-04,42
2011-11-04,41
"
z1=read.zoo(textConnection(csv),sep=',')
w1=to.weekly(z1)
ep=endpoints(z1,"weeks",1)
w1$Volume=period.apply(z1,ep,length)
z2=read.zoo(textConnection(csv),sep=',',aggregate=T)
w2=to.weekly(z2)
ep=endpoints(z2,"weeks",1)
w2$Volume=period.apply(z2,ep,length)
vignette('zoo-faq'), entry 1,告诉我aggregate=T 摆脱了zoo 烦人的警告信息.但随后结果发生了变化:
vignette('zoo-faq'), entry 1, tells me aggregate=T gets rid of zoo's annoying warning message. But then the results change:
> w1
z1.Open z1.High z1.Low z1.Close Volume
2011-11-04 50 50 41 41 10
> w2
z2.Open z2.High z2.Low z2.Close Volume
2011-11-04 50 50 42.5 42.5 4
是否有另一种方法可以消除警告消息,但仍然获得与 w1 相同的结果?(是的,我知道suppressWarnings(),这是我之前使用的,但我讨厌这个想法.)(我想知道如何将自定义聚合函数传递给 read.zoo,它会返回每天的 OHLCV 数据……但如果可能的话,我什至无法解决.)
Is there another way to get rid of the warning message but still get the same results as w1? (Yes, I know about suppressWarnings(), which is what I was using before, but I hate the idea.)(I was wondering about passing a custom aggregate function to read.zoo that would return OHLCV data for each day... but couldn't even work out if that was even possible.)
推荐答案
就像 Dirk 建议的一个简单变体一样,这应该可行
Just as a simple variant on Dirk's suggestion, this should work
z0 = read.csv( textConnection(csv), sep=',', header=FALSE )
z1 = zoo( z0$V2, as.Date(z0$V1) + (1:nrow(z0))*10^-10 )
这篇关于R/zoo:处理非唯一索引条目但不丢失数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!