我很高兴使用这段代码:
z=lapply(filename_list, function(fname){
read.zoo(file=fname,header=TRUE,sep = ",",tz = "")
})
xts( do.call(rbind,z) )
Run Code Online (Sandbox Code Playgroud)
直到Dirty Data在一个文件的末尾出现:
Open High Low Close Volume
2011-09-20 21:00:00 1.370105 1.370105 1.370105 1.370105 1
Run Code Online (Sandbox Code Playgroud)
这是在下一个文件的开头:
Open High Low Close Volume
2011-09-20 21:00:00 1.370105 1.371045 1.369685 1.3702 2230
Run Code Online (Sandbox Code Playgroud)
所以rbind.zoo抱怨重复.
我不能使用像:
y <- x[ ! duplicated( index(x) ), ]
Run Code Online (Sandbox Code Playgroud)
因为它们在不同的动物园对象中,在列表中.我不能aggregate像这里建议的那样使用,因为它们是动物园对象的列表,而不是一个大的动物园对象.而且我无法得到一个重要的对象.第二十二条军规.
所以,当事情变得艰难时,强硬的黑客攻击一些for循环(原谅印刷品和停止,因为这还不是工作代码):
indexes <- do.call("c", unname(lapply(z, index)))
dups=duplicated(indexes)
if(any(dups)){
duplicate_timestamps=indexes[dups]
for(tix in 1:length(duplicate_timestamps)){
t=duplicate_timestamps[tix]
print("We have a duplicate:");print(t)
for(zix in 1:length(z)){
if(t %in% index(z[[zix]])){
print(z[[zix]][t])
if(z[[zix]][t]$Volume==1){
print("-->Deleting this one");
z[[zix]][t]=NULL #<-- PROBLEM
}
}
}
}
stop("There are duplicate bars!!")
}
Run Code Online (Sandbox Code Playgroud)
我难以理解的是为动物园行分配NULL不会删除它(NextMethod中的错误("[< - "):替换的长度为零).好的,所以我做了一个过滤器副本,没有违规的项目...但我正在绊倒这些:
> z[[zix]][!t,]
Error in Ops.POSIXt(t) : unary '!' not defined for "POSIXt" objects
> z[[zix]][-t,]
Error in `-.POSIXt`(t) : unary '-' is not defined for "POSIXt" objects
Run Code Online (Sandbox Code Playgroud)
PS虽然非常欢迎我在"动物园对象列表中复制行"的真正问题的高级解决方案,但这里的问题具体是关于如何在给定POSIXt索引对象的情况下从动物园对象中删除行.
一小部分测试数据:
list(structure(c(1.36864, 1.367045, 1.370105, 1.36928, 1.37039,
1.370105, 1.36604, 1.36676, 1.370105, 1.367065, 1.37009, 1.370105,
5498, 3244, 1), .Dim = c(3L, 5L), .Dimnames = list(NULL, c("Open",
"High", "Low", "Close", "Volume")), index = structure(c(1316512800,
1316516400, 1316520000), class = c("POSIXct", "POSIXt"), tzone = ""), class = "zoo"),
structure(c(1.370105, 1.370115, 1.36913, 1.371045, 1.37023,
1.37075, 1.369685, 1.36847, 1.367885, 1.3702, 1.36917, 1.37061,
2230, 2909, 2782), .Dim = c(3L, 5L), .Dimnames = list(NULL,
c("Open", "High", "Low", "Close", "Volume")), index = structure(c(1316520000,
1316523600, 1316527200), class = c("POSIXct", "POSIXt"), tzone = ""), class = "zoo"))
Run Code Online (Sandbox Code Playgroud)
更新:感谢G. Grothendieck的行删除解决方案.在实际代码中,我遵循Joshua和GSee的建议来获取xts对象的列表而不是动物园对象列表.所以我的代码成了:
z=lapply(filename_list, function(fname){
xts(read.zoo(file=fname,header=TRUE,sep = ",",tz = ""))
})
x=do.call.rbind(z)
Run Code Online (Sandbox Code Playgroud)
(顺便说一句,请注意电话do.call.rbind.这是因为rbind.xts有一些严重的内存问题.请参阅/sf/answers/842055651/)
然后我删除重复项作为后处理步骤:
dups=duplicated(index(x))
if(any(dups)){
duplicate_timestamps=index(x)[dups]
to_delete=x[ (index(x) %in% duplicate_timestamps) & x$Volume<=1]
if(nrow(to_delete)>0){
#Next line says all lines that are not in the duplicate_timestamp group
# OR are in the duplicate timestamps, but have a volume greater than 1.
print("Will delete the volume=1 entry")
x=x[ !(index(x) %in% duplicate_timestamps) | x$Volume>1]
}else{
stop("Duplicate timestamps, and we cannot easily remove them just based on low volume.")
}
}
Run Code Online (Sandbox Code Playgroud)
如果z1和z2是你的动物园对象然后rbind同时去除任何重复z2:
rbind( z1, z2[ ! time(z2) %in% time(z1) ] )
Run Code Online (Sandbox Code Playgroud)
关于删除具有指定时间的动物园对象中的点,上面已经说明了这一点,但一般来说,如果tt是要删除的时间向量:
z[ ! time(z) %in% tt ]
Run Code Online (Sandbox Code Playgroud)
或者,如果我们知道有一个单一的元素tt,然后z[ time(z) != tt ].
| 归档时间: |
|
| 查看次数: |
8164 次 |
| 最近记录: |