1 분 시간대에 발생하는 데이터 간격을 나타내는 data.frame을 사용하는 함수를 작성했습니다. 함수의 목적은이 1 분 간격을 취하여 더 높은 간격으로 변환하는 것입니다. 예를 들어, 1 분이 5 분, 60 분 등이됩니다. 데이터 세트 자체는 데이터에 갭을 갖게 될 가능성이 있습니다. 즉 시간상으로 점프하여 이러한 잘못된 데이터 발생을 수용해야합니다. 나는 작동하는 것처럼 보이는 다음 코드를 작성했지만 성능은 대용량 데이터 세트에서 절대적으로 끔찍합니다.타임 스탬프 된 행 데이터를 변환 할 때 성능 문제가 발생했습니다.
나는 누군가가 내가 이것을 가속화 할 수있는 방법에 대해 제안 할 수 있기를 바라고있다. 아래를 참조하십시오.
compressMinute = function(interval, DAT) {
#Grab all data which begins at the same interval length
retSet = NULL
intervalFilter = which(DAT$time$min %% interval == 0)
barSet = NULL
for (x in intervalFilter) {
barEndTime = DAT$time[x] + 60*interval
barIntervals = DAT[x,]
x = x+1
while(x <= nrow(DAT) & DAT[x,"time"] < barEndTime) {
barIntervals = rbind(barIntervals,DAT[x,])
x = x + 1
}
bar = data.frame(date=barIntervals[1,"date"],time=barIntervals[1,"time"],open=barIntervals[1,"open"],high=max(barIntervals[1:nrow(barIntervals),"high"]),
low=min(barIntervals[1:nrow(barIntervals),"low"]),close=tail(barIntervals,1)$close,volume=sum(barIntervals[1:nrow(barIntervals),"volume"]))
if (is.null(barSet)) {
barSet = bar
} else {
barSet = rbind(barSet, bar)
}
}
return(barSet)
}
편집 : 아래
내 데이터의 행입니다. 각 행은 1 분 간격을 나타냅니다.이 값을이 1 분 간격의 집계 인 임의의 버킷 (예 : 5 분, 15 분, 60 분, 240 분 등)으로 변환하려고합니다.
date time open high low close volume
2005-09-06 2005-09-06 16:33:00 1297.25 1297.50 1297.25 1297.25 98
'barIntervals = rbind (barIntervals, DAT [x,])'줄은 병목 현상이 아닙니다. barIntervals 객체를 반복 할 때마다 R이 복사본을 만들어야합니다 ... barIntervals를 미리 할당하면 훨씬 더 잘 작동합니다. 더 많은 것을 읽는 Google'R preallocation'. 또한, 패키지'zoo'의'na.approx()'함수가 원하는대로 할 수 있습니까? 이 질문을 확인하십시오 : http://stackoverflow.com/questions/7188807/interpolate-na-values – Chase
발견 된 XTS. 큰 차이. – Dave