根据 R 中的每日价格计算每周 returns

Calculate weekly returns from daily prices In R

我想从每天开始计算每周 returns(使用周三到周三的惯例)。原始数据如下(摘录):

...
4003  1985-05-06       200.764
4004  1985-05-07       202.502
4005  1985-05-08       202.683
4006  1985-05-09       204.642
4007  1985-05-10       206.051
4008  1985-05-13       207.702
4009  1985-05-14       207.630
4010  1985-05-15       207.585
4011  1985-05-16       207.843
4012  1985-05-17       209.723
4013  1985-05-20       212.843
...

为了计算每周returns,我想提取每个星期三的数据。如果一周不包括星期三,我想提取下一个工作日。

为了提取星期三的数据,我使用了以下代码:

wednesday = as.POSIXlt(time(data))$wday == 3
indx <- c(0, which(wednesday))
datanew<-period.apply(data, INDEX=indx, FUN=last)

但是用这段代码,如果这周没有星期三的数据,显然不会提取星期三的数据,也就是说星期三放假。

有人可以帮忙吗?

这里是我的计算向量 idx 的建议,它可以用作 period.applyINDEX 参数:

#-------------------------------------------------------------------
# Example data:

set.seed(1)
N <- 1000
data <- data.frame( t = as.Date("1985-01-01") + (0:(N-1))*as.difftime(1,units="days"),
                    x = sample(100:300,N,replace=TRUE))

weekDay <- function(t=data$t){as.POSIXlt(t)$wday}

# Remove Saterdays, Sundays, and 20% of the rest:
data <- data[-which(weekDay() %in% c(0,6)),]
data <- data[-sample(1:nrow(data),ceiling(0.2*nrow(data))),]

#--------------------------------------------------------------------------
# Count the number of weeks:
dt <- data$t[nrow(data)] - data$t[1]
units(dt) <- "days"
weekCnt <- ceiling(as.numeric(dt)/7)

#--------------------------------------------------------------------------
# Find all Wednesdays in the interval from data$t[1] to data$t[nrow(data)]:
wed.0 <- data$t[1] + ((3-weekDay(data$t[1])) %% 7)*as.difftime(1,units="days")
wed <- wed.0 + (0:(weekCnt-1))*as.difftime(7,units="days")

#--------------------------------------------------------------------------
# For each week find the the smallest index i such that data$t[i] is not
# earlier than the Wednesday of that week:
idx <- rep(NA,weekCnt)
for ( i in 1:weekCnt ) { idx[i] <- which.max( wed[i] <= data$t ) }

#--------------------------------------------------------------------------
# Check the result:
X <- cbind( data, weekDay(), week=strftime(data$t,format="%W"), selected=rep(FALSE,nrow(data)))
X$selected[idx] <- TRUE

(我对 for-loop 感到抱歉。但是里面没有任何东西在生长,所以这是一个无害的 for-loop。) 结果总结在X:

> X
             t   x weekDay() week selected
1   1985-01-01 153         2   00    FALSE
3   1985-01-03 215         4   00     TRUE
7   1985-01-07 289         1   01    FALSE
9   1985-01-09 226         3   01     TRUE
10  1985-01-10 112         4   01    FALSE
14  1985-01-14 177         1   02    FALSE
15  1985-01-15 254         2   02    FALSE
16  1985-01-16 200         3   02     TRUE
17  1985-01-17 244         4   02    FALSE
18  1985-01-18 299         5   02    FALSE
21  1985-01-21 287         1   03    FALSE
22  1985-01-22 142         2   03    FALSE
23  1985-01-23 230         3   03     TRUE
25  1985-01-25 153         5   03    FALSE
28  1985-01-28 176         1   04    FALSE
30  1985-01-30 168         3   04     TRUE
31  1985-01-31 196         4   04    FALSE
32  1985-02-01 220         5   04    FALSE
35  1985-02-04 266         1   05    FALSE
36  1985-02-05 234         2   05    FALSE
37  1985-02-06 259         3   05     TRUE
39  1985-02-08 245         5   05    FALSE
42  1985-02-11 230         1   06    FALSE
44  1985-02-13 211         3   06     TRUE
45  1985-02-14 206         4   06    FALSE
46  1985-02-15 258         5   06    FALSE
49  1985-02-18 247         1   07    FALSE
50  1985-02-19 239         2   07    FALSE
51  1985-02-20 196         3   07     TRUE
52  1985-02-21 273         4   07    FALSE
53  1985-02-22 188         5   07    FALSE
56  1985-02-25 119         1   08    FALSE
57  1985-02-26 163         2   08    FALSE
58  1985-02-27 204         3   08     TRUE
59  1985-02-28 233         4   08    FALSE
60  1985-03-01 181         5   08    FALSE
63  1985-03-04 192         1   09    FALSE
64  1985-03-05 166         2   09    FALSE
65  1985-03-06 230         3   09     TRUE
66  1985-03-07 151         4   09    FALSE
67  1985-03-08 196         5   09    FALSE
70  1985-03-11 275         1   10    FALSE
71  1985-03-12 168         2   10    FALSE
72  1985-03-13 268         3   10     TRUE
74  1985-03-15 167         5   10    FALSE
77  1985-03-18 273         1   11    FALSE
78  1985-03-19 178         2   11    FALSE
79  1985-03-20 256         3   11     TRUE
80  1985-03-21 293         4   11    FALSE
81  1985-03-22 187         5   11    FALSE
85  1985-03-26 252         2   12    FALSE
86  1985-03-27 140         3   12     TRUE
87  1985-03-28 242         4   12    FALSE
88  1985-03-29 124         5   12    FALSE
92  1985-04-02 111         2   13    FALSE
94  1985-04-04 276         4   13     TRUE
95  1985-04-05 256         5   13    FALSE
98  1985-04-08 182         1   14    FALSE
100 1985-04-10 221         3   14     TRUE
101 1985-04-11 231         4   14    FALSE
102 1985-04-12 170         5   14    FALSE
105 1985-04-15 227         1   15    FALSE
108 1985-04-18 196         4   15     TRUE
109 1985-04-19 285         5   15    FALSE
112 1985-04-22 247         1   16    FALSE
114 1985-04-24 186         3   16     TRUE
115 1985-04-25 129         4   16    FALSE
116 1985-04-26 102         5   16    FALSE
120 1985-04-30 228         2   17    FALSE
121 1985-05-01 299         3   17     TRUE
122 1985-05-02 199         4   17    FALSE
123 1985-05-03 197         5   17    FALSE
126 1985-05-06 191         1   18    FALSE
127 1985-05-07 202         2   18    FALSE
130 1985-05-10 219         5   18     TRUE
133 1985-05-13 107         1   19    FALSE
134 1985-05-14 229         2   19    FALSE
135 1985-05-15 286         3   19     TRUE
136 1985-05-16 220         4   19    FALSE
137 1985-05-17 212         5   19    FALSE
140 1985-05-20 202         1   20    FALSE
141 1985-05-21 237         2   20    FALSE
142 1985-05-22 220         3   20     TRUE
148 1985-05-28 250         2   21    FALSE
151 1985-05-31 223         5   21     TRUE
155 1985-06-04 200         2   22    FALSE
156 1985-06-05 136         3   22     TRUE
157 1985-06-06 206         4   22    FALSE
158 1985-06-07 115         5   22    FALSE
161 1985-06-10 157         1   23    FALSE
163 1985-06-12 189         3   23     TRUE
164 1985-06-13 256         4   23    FALSE
165 1985-06-14 277         5   23    FALSE
168 1985-06-17 167         1   24    FALSE
170 1985-06-19 167         3   24     TRUE
171 1985-06-20 226         4   24    FALSE
172 1985-06-21 268         5   24    FALSE
175 1985-06-24 176         1   25    FALSE
176 1985-06-25 279         2   25    FALSE
178 1985-06-27 248         4   25     TRUE
179 1985-06-28 221         5   25    FALSE
182 1985-07-01 138         1   26    FALSE
183 1985-07-02 278         2   26    FALSE
184 1985-07-03 201         3   26     TRUE
185 1985-07-04 276         4   26    FALSE
189 1985-07-08 289         1   27    FALSE
190 1985-07-09 210         2   27    FALSE
191 1985-07-10 243         3   27     TRUE
192 1985-07-11 178         4   27    FALSE
196 1985-07-15 218         1   28    FALSE
197 1985-07-16 122         2   28    FALSE
198 1985-07-17 268         3   28     TRUE
200 1985-07-19 257         5   28    FALSE
203 1985-07-22 203         1   29    FALSE
204 1985-07-23 154         2   29    FALSE
205 1985-07-24 136         3   29     TRUE
206 1985-07-25 204         4   29    FALSE
207 1985-07-26 213         5   29    FALSE
210 1985-07-29 244         1   30    FALSE
212 1985-07-31 120         3   30     TRUE
213 1985-08-01 253         4   30    FALSE
217 1985-08-05 230         1   31    FALSE
219 1985-08-07 291         3   31     TRUE
220 1985-08-08 168         4   31    FALSE
221 1985-08-09 152         5   31    FALSE
225 1985-08-13 285         2   32    FALSE
226 1985-08-14 202         3   32     TRUE
227 1985-08-15 151         4   32    FALSE
231 1985-08-19 169         1   33    FALSE
233 1985-08-21 175         3   33     TRUE
234 1985-08-22 226         4   33    FALSE
235 1985-08-23 178         5   33    FALSE
238 1985-08-26 211         1   34    FALSE
239 1985-08-27 186         2   34    FALSE
240 1985-08-28 190         3   34     TRUE
241 1985-08-29 161         4   34    FALSE
242 1985-08-30 216         5   34    FALSE
245 1985-09-02 183         1   35    FALSE
246 1985-09-03 142         2   35    FALSE
247 1985-09-04 186         3   35     TRUE
248 1985-09-05 126         4   35    FALSE
249 1985-09-06 192         5   35    FALSE
252 1985-09-09 287         1   36    FALSE
253 1985-09-10 194         2   36    FALSE
255 1985-09-12 197         4   36     TRUE
256 1985-09-13 121         5   36    FALSE
260 1985-09-17 287         2   37    FALSE
263 1985-09-20 155         5   37     TRUE
266 1985-09-23 133         1   38    FALSE
267 1985-09-24 112         2   38    FALSE
268 1985-09-25 251         3   38     TRUE
269 1985-09-26 224         4   38    FALSE
273 1985-09-30 176         1   39    FALSE
274 1985-10-01 134         2   39    FALSE
275 1985-10-02 160         3   39     TRUE
276 1985-10-03 138         4   39    FALSE
277 1985-10-04 151         5   39    FALSE
280 1985-10-07 254         1   40    FALSE
281 1985-10-08 105         2   40    FALSE
282 1985-10-09 205         3   40     TRUE
283 1985-10-10 276         4   40    FALSE
284 1985-10-11 174         5   40    FALSE
287 1985-10-14 164         1   41    FALSE
288 1985-10-15 131         2   41    FALSE
290 1985-10-17 144         4   41     TRUE
291 1985-10-18 145         5   41    FALSE
294 1985-10-21 165         1   42    FALSE
297 1985-10-24 119         4   42     TRUE
298 1985-10-25 123         5   42    FALSE
302 1985-10-29 119         2   43    FALSE
303 1985-10-30 199         3   43     TRUE
304 1985-10-31 192         4   43    FALSE
305 1985-11-01 175         5   43    FALSE
308 1985-11-04 263         1   44    FALSE
309 1985-11-05 113         2   44    FALSE
310 1985-11-06 180         3   44     TRUE
311 1985-11-07 128         4   44    FALSE
312 1985-11-08 138         5   44    FALSE
315 1985-11-11 153         1   45    FALSE
316 1985-11-12 199         2   45    FALSE
317 1985-11-13 116         3   45     TRUE
318 1985-11-14 171         4   45    FALSE
319 1985-11-15 294         5   45    FALSE
324 1985-11-20 300         3   46     TRUE
326 1985-11-22 291         5   46    FALSE
331 1985-11-27 298         3   47     TRUE
332 1985-11-28 159         4   47    FALSE
333 1985-11-29 180         5   47    FALSE
336 1985-12-02 173         1   48    FALSE
337 1985-12-03 188         2   48    FALSE
340 1985-12-06 295         5   48     TRUE
343 1985-12-09 208         1   49    FALSE
345 1985-12-11 235         3   49     TRUE
346 1985-12-12 154         4   49    FALSE
347 1985-12-13 194         5   49    FALSE
350 1985-12-16 245         1   50    FALSE
351 1985-12-17 197         2   50    FALSE
353 1985-12-19 257         4   50     TRUE
357 1985-12-23 270         1   51    FALSE
358 1985-12-24 116         2   51    FALSE
360 1985-12-26 194         4   51     TRUE
364 1985-12-30 155         1   52    FALSE
365 1985-12-31 217         2   52    FALSE
367 1986-01-02 114         4   00     TRUE
368 1986-01-03 241         5   00    FALSE
372 1986-01-07 212         2   01    FALSE
373 1986-01-08 286         3   01     TRUE
374 1986-01-09 146         4   01    FALSE
375 1986-01-10 144         5   01    FALSE
378 1986-01-13 273         1   02    FALSE
379 1986-01-14 135         2   02    FALSE
380 1986-01-15 199         3   02     TRUE
382 1986-01-17 213         5   02    FALSE
385 1986-01-20 146         1   03    FALSE
386 1986-01-21 148         2   03    FALSE
387 1986-01-22 260         3   03     TRUE
388 1986-01-23 267         4   03    FALSE
389 1986-01-24 122         5   03    FALSE
392 1986-01-27 128         1   04    FALSE
393 1986-01-28 285         2   04    FALSE
395 1986-01-30 131         4   04     TRUE
396 1986-01-31 170         5   04    FALSE
399 1986-02-03 170         1   05    FALSE
400 1986-02-04 129         2   05    FALSE
401 1986-02-05 232         3   05     TRUE
402 1986-02-06 137         4   05    FALSE
403 1986-02-07 291         5   05    FALSE
406 1986-02-10 245         1   06    FALSE
408 1986-02-12 256         3   06     TRUE
409 1986-02-13 102         4   06    FALSE
413 1986-02-17 250         1   07    FALSE
415 1986-02-19 241         3   07     TRUE
416 1986-02-20 195         4   07    FALSE
420 1986-02-24 265         1   08    FALSE
421 1986-02-25 187         2   08    FALSE
422 1986-02-26 203         3   08     TRUE
423 1986-02-27 233         4   08    FALSE
427 1986-03-03 117         1   09    FALSE
428 1986-03-04 287         2   09    FALSE
429 1986-03-05 268         3   09     TRUE
430 1986-03-06 276         4   09    FALSE
431 1986-03-07 288         5   09    FALSE
434 1986-03-10 208         1   10    FALSE
435 1986-03-11 121         2   10    FALSE
436 1986-03-12 261         3   10     TRUE
437 1986-03-13 248         4   10    FALSE
438 1986-03-14 110         5   10    FALSE
441 1986-03-17 108         1   11    FALSE
442 1986-03-18 159         2   11    FALSE
443 1986-03-19 200         3   11     TRUE
444 1986-03-20 222         4   11    FALSE
448 1986-03-24 289         1   12    FALSE
450 1986-03-26 114         3   12     TRUE
451 1986-03-27 293         4   12    FALSE
455 1986-03-31 110         1   13    FALSE
456 1986-04-01 232         2   13    FALSE
457 1986-04-02 216         3   13     TRUE
458 1986-04-03 298         4   13    FALSE
459 1986-04-04 221         5   13    FALSE
462 1986-04-07 195         1   14    FALSE
463 1986-04-08 100         2   14    FALSE
464 1986-04-09 188         3   14     TRUE
465 1986-04-10 152         4   14    FALSE
466 1986-04-11 288         5   14    FALSE
469 1986-04-14 195         1   15    FALSE
470 1986-04-15 238         2   15    FALSE
472 1986-04-17 291         4   15     TRUE
473 1986-04-18 243         5   15    FALSE
476 1986-04-21 148         1   16    FALSE
478 1986-04-23 187         3   16     TRUE
479 1986-04-24 200         4   16    FALSE
480 1986-04-25 239         5   16    FALSE
483 1986-04-28 270         1   17    FALSE
485 1986-04-30 218         3   17     TRUE
487 1986-05-02 138         5   17    FALSE
490 1986-05-05 126         1   18    FALSE
491 1986-05-06 104         2   18    FALSE
492 1986-05-07 288         3   18     TRUE
493 1986-05-08 158         4   18    FALSE
494 1986-05-09 133         5   18    FALSE
498 1986-05-13 203         2   19    FALSE
499 1986-05-14 270         3   19     TRUE
501 1986-05-16 211         5   19    FALSE
505 1986-05-20 194         2   20    FALSE
506 1986-05-21 294         3   20     TRUE
507 1986-05-22 180         4   20    FALSE
508 1986-05-23 270         5   20    FALSE
511 1986-05-26 275         1   21    FALSE
513 1986-05-28 101         3   21     TRUE
514 1986-05-29 246         4   21    FALSE
515 1986-05-30 244         5   21    FALSE
518 1986-06-02 208         1   22    FALSE
519 1986-06-03 167         2   22    FALSE
520 1986-06-04 228         3   22     TRUE
521 1986-06-05 266         4   22    FALSE
522 1986-06-06 242         5   22    FALSE
525 1986-06-09 178         1   23    FALSE
526 1986-06-10 286         2   23    FALSE
527 1986-06-11 261         3   23     TRUE
528 1986-06-12 252         4   23    FALSE
532 1986-06-16 105         1   24    FALSE
534 1986-06-18 155         3   24     TRUE
535 1986-06-19 123         4   24    FALSE
536 1986-06-20 108         5   24    FALSE
539 1986-06-23 134         1   25    FALSE
540 1986-06-24 224         2   25    FALSE
541 1986-06-25 179         3   25     TRUE
542 1986-06-26 292         4   25    FALSE
543 1986-06-27 231         5   25    FALSE
547 1986-07-01 300         2   26    FALSE
548 1986-07-02 176         3   26     TRUE
549 1986-07-03 212         4   26    FALSE
550 1986-07-04 247         5   26    FALSE
553 1986-07-07 102         1   27    FALSE
554 1986-07-08 282         2   27    FALSE
555 1986-07-09 254         3   27     TRUE
557 1986-07-11 118         5   27    FALSE
560 1986-07-14 266         1   28    FALSE
561 1986-07-15 231         2   28    FALSE
563 1986-07-17 168         4   28     TRUE
568 1986-07-22 229         2   29    FALSE
569 1986-07-23 156         3   29     TRUE
570 1986-07-24 292         4   29    FALSE
574 1986-07-28 118         1   30    FALSE
575 1986-07-29 266         2   30    FALSE
576 1986-07-30 205         3   30     TRUE
578 1986-08-01 182         5   30    FALSE
581 1986-08-04 169         1   31    FALSE
582 1986-08-05 290         2   31    FALSE
584 1986-08-07 107         4   31     TRUE
585 1986-08-08 219         5   31    FALSE
588 1986-08-11 206         1   32    FALSE
589 1986-08-12 293         2   32    FALSE
590 1986-08-13 242         3   32     TRUE
591 1986-08-14 211         4   32    FALSE
595 1986-08-18 266         1   33    FALSE
596 1986-08-19 230         2   33    FALSE
597 1986-08-20 196         3   33     TRUE
598 1986-08-21 199         4   33    FALSE
599 1986-08-22 176         5   33    FALSE
602 1986-08-25 286         1   34    FALSE
603 1986-08-26 129         2   34    FALSE
604 1986-08-27 250         3   34     TRUE
605 1986-08-28 296         4   34    FALSE
606 1986-08-29 295         5   34    FALSE
609 1986-09-01 291         1   35    FALSE
610 1986-09-02 121         2   35    FALSE
612 1986-09-04 169         4   35     TRUE
618 1986-09-10 168         3   36     TRUE
619 1986-09-11 225         4   36    FALSE
620 1986-09-12 111         5   36    FALSE
624 1986-09-16 127         2   37    FALSE
625 1986-09-17 165         3   37     TRUE
627 1986-09-19 151         5   37    FALSE
630 1986-09-22 288         1   38    FALSE
631 1986-09-23 272         2   38    FALSE
632 1986-09-24 174         3   38     TRUE
633 1986-09-25 163         4   38    FALSE
634 1986-09-26 266         5   38    FALSE
637 1986-09-29 119         1   39    FALSE
638 1986-09-30 113         2   39    FALSE
639 1986-10-01 238         3   39     TRUE
640 1986-10-02 234         4   39    FALSE
644 1986-10-06 140         1   40    FALSE
647 1986-10-09 106         4   40     TRUE
651 1986-10-13 235         1   41    FALSE
652 1986-10-14 269         2   41    FALSE
653 1986-10-15 172         3   41     TRUE
654 1986-10-16 178         4   41    FALSE
655 1986-10-17 214         5   41    FALSE
658 1986-10-20 218         1   42    FALSE
660 1986-10-22 274         3   42     TRUE
661 1986-10-23 174         4   42    FALSE
665 1986-10-27 171         1   43    FALSE
667 1986-10-29 283         3   43     TRUE
669 1986-10-31 174         5   43    FALSE
672 1986-11-03 204         1   44    FALSE
673 1986-11-04 266         2   44    FALSE
674 1986-11-05 205         3   44     TRUE
676 1986-11-07 184         5   44    FALSE
679 1986-11-10 159         1   45    FALSE
680 1986-11-11 155         2   45    FALSE
681 1986-11-12 254         3   45     TRUE
682 1986-11-13 256         4   45    FALSE
686 1986-11-17 201         1   46    FALSE
687 1986-11-18 177         2   46    FALSE
688 1986-11-19 185         3   46     TRUE
690 1986-11-21 284         5   46    FALSE
694 1986-11-25 220         2   47    FALSE
695 1986-11-26 185         3   47     TRUE
696 1986-11-27 212         4   47    FALSE
697 1986-11-28 258         5   47    FALSE
700 1986-12-01 195         1   48    FALSE
701 1986-12-02 286         2   48    FALSE
702 1986-12-03 281         3   48     TRUE
703 1986-12-04 250         4   48    FALSE
704 1986-12-05 235         5   48    FALSE
708 1986-12-09 206         2   49    FALSE
709 1986-12-10 289         3   49     TRUE
710 1986-12-11 243         4   49    FALSE
711 1986-12-12 245         5   49    FALSE
714 1986-12-15 257         1   50    FALSE
715 1986-12-16 188         2   50    FALSE
716 1986-12-17 186         3   50     TRUE
718 1986-12-19 129         5   50    FALSE
721 1986-12-22 100         1   51    FALSE
722 1986-12-23 221         2   51    FALSE
723 1986-12-24 282         3   51     TRUE
724 1986-12-25 242         4   51    FALSE
725 1986-12-26 152         5   51    FALSE
728 1986-12-29 216         1   52    FALSE
729 1986-12-30 186         2   52    FALSE
730 1986-12-31 110         3   52     TRUE
731 1987-01-01 246         4   00    FALSE
732 1987-01-02 210         5   00    FALSE
735 1987-01-05 243         1   01    FALSE
736 1987-01-06 159         2   01    FALSE
738 1987-01-08 266         4   01     TRUE
739 1987-01-09 117         5   01    FALSE
742 1987-01-12 209         1   02    FALSE
743 1987-01-13 222         2   02    FALSE
745 1987-01-15 141         4   02     TRUE
746 1987-01-16 176         5   02    FALSE
749 1987-01-19 124         1   03    FALSE
750 1987-01-20 235         2   03    FALSE
751 1987-01-21 199         3   03     TRUE
752 1987-01-22 281         4   03    FALSE
753 1987-01-23 210         5   03    FALSE
756 1987-01-26 138         1   04    FALSE
757 1987-01-27 187         2   04    FALSE
758 1987-01-28 145         3   04     TRUE
759 1987-01-29 293         4   04    FALSE
760 1987-01-30 190         5   04    FALSE
763 1987-02-02 274         1   05    FALSE
764 1987-02-03 141         2   05    FALSE
765 1987-02-04 135         3   05     TRUE
766 1987-02-05 133         4   05    FALSE
767 1987-02-06 213         5   05    FALSE
770 1987-02-09 242         1   06    FALSE
771 1987-02-10 195         2   06    FALSE
772 1987-02-11 264         3   06     TRUE
773 1987-02-12 103         4   06    FALSE
774 1987-02-13 300         5   06    FALSE
777 1987-02-16 105         1   07    FALSE
778 1987-02-17 251         2   07    FALSE
779 1987-02-18 141         3   07     TRUE
780 1987-02-19 300         4   07    FALSE
781 1987-02-20 282         5   07    FALSE
784 1987-02-23 194         1   08    FALSE
785 1987-02-24 273         2   08    FALSE
786 1987-02-25 133         3   08     TRUE
787 1987-02-26 224         4   08    FALSE
788 1987-02-27 158         5   08    FALSE
791 1987-03-02 135         1   09    FALSE
792 1987-03-03 111         2   09    FALSE
793 1987-03-04 289         3   09     TRUE
795 1987-03-06 204         5   09    FALSE
799 1987-03-10 262         2   10    FALSE
800 1987-03-11 170         3   10     TRUE
805 1987-03-16 154         1   11    FALSE
806 1987-03-17 236         2   11    FALSE
808 1987-03-19 290         4   11     TRUE
809 1987-03-20 168         5   11    FALSE
812 1987-03-23 177         1   12    FALSE
813 1987-03-24 171         2   12    FALSE
816 1987-03-27 209         5   12     TRUE
819 1987-03-30 148         1   13    FALSE
820 1987-03-31 244         2   13    FALSE
821 1987-04-01 170         3   13     TRUE
822 1987-04-02 211         4   13    FALSE
823 1987-04-03 249         5   13    FALSE
826 1987-04-06 107         1   14    FALSE
827 1987-04-07 144         2   14    FALSE
828 1987-04-08 173         3   14     TRUE
830 1987-04-10 246         5   14    FALSE
833 1987-04-13 269         1   15    FALSE
834 1987-04-14 256         2   15    FALSE
836 1987-04-16 215         4   15     TRUE
837 1987-04-17 115         5   15    FALSE
841 1987-04-21 285         2   16    FALSE
842 1987-04-22 127         3   16     TRUE
843 1987-04-23 147         4   16    FALSE
848 1987-04-28 229         2   17    FALSE
849 1987-04-29 243         3   17     TRUE
850 1987-04-30 170         4   17    FALSE
851 1987-05-01 290         5   17    FALSE
854 1987-05-04 297         1   18    FALSE
855 1987-05-05 143         2   18    FALSE
856 1987-05-06 107         3   18     TRUE
861 1987-05-11 104         1   19    FALSE
862 1987-05-12 153         2   19    FALSE
863 1987-05-13 239         3   19     TRUE
864 1987-05-14 106         4   19    FALSE
865 1987-05-15 112         5   19    FALSE
868 1987-05-18 226         1   20    FALSE
869 1987-05-19 245         2   20    FALSE
870 1987-05-20 115         3   20     TRUE
871 1987-05-21 184         4   20    FALSE
872 1987-05-22 295         5   20    FALSE
876 1987-05-26 102         2   21    FALSE
878 1987-05-28 191         4   21     TRUE
879 1987-05-29 248         5   21    FALSE
882 1987-06-01 289         1   22    FALSE
883 1987-06-02 293         2   22    FALSE
884 1987-06-03 280         3   22     TRUE
885 1987-06-04 199         4   22    FALSE
886 1987-06-05 257         5   22    FALSE
889 1987-06-08 216         1   23    FALSE
890 1987-06-09 166         2   23    FALSE
891 1987-06-10 100         3   23     TRUE
892 1987-06-11 113         4   23    FALSE
893 1987-06-12 117         5   23    FALSE
896 1987-06-15 123         1   24    FALSE
898 1987-06-17 107         3   24     TRUE
899 1987-06-18 101         4   24    FALSE
900 1987-06-19 132         5   24    FALSE
903 1987-06-22 154         1   25    FALSE
904 1987-06-23 137         2   25    FALSE
905 1987-06-24 145         3   25     TRUE
906 1987-06-25 112         4   25    FALSE
907 1987-06-26 112         5   25    FALSE
910 1987-06-29 108         1   26    FALSE
911 1987-06-30 205         2   26    FALSE
912 1987-07-01 258         3   26     TRUE
913 1987-07-02 239         4   26    FALSE
914 1987-07-03 113         5   26    FALSE
917 1987-07-06 131         1   27    FALSE
918 1987-07-07 244         2   27    FALSE
919 1987-07-08 241         3   27     TRUE
920 1987-07-09 277         4   27    FALSE
921 1987-07-10 165         5   27    FALSE
925 1987-07-14 279         2   28    FALSE
926 1987-07-15 265         3   28     TRUE
927 1987-07-16 245         4   28    FALSE
928 1987-07-17 158         5   28    FALSE
932 1987-07-21 286         2   29    FALSE
933 1987-07-22 148         3   29     TRUE
935 1987-07-24 247         5   29    FALSE
938 1987-07-27 193         1   30    FALSE
939 1987-07-28 272         2   30    FALSE
941 1987-07-30 141         4   30     TRUE
945 1987-08-03 191         1   31    FALSE
946 1987-08-04 183         2   31    FALSE
947 1987-08-05 234         3   31     TRUE
952 1987-08-10 135         1   32    FALSE
953 1987-08-11 229         2   32    FALSE
954 1987-08-12 275         3   32     TRUE
956 1987-08-14 152         5   32    FALSE
959 1987-08-17 198         1   33    FALSE
961 1987-08-19 172         3   33     TRUE
962 1987-08-20 188         4   33    FALSE
963 1987-08-21 125         5   33    FALSE
966 1987-08-24 148         1   34    FALSE
967 1987-08-25 172         2   34    FALSE
968 1987-08-26 279         3   34     TRUE
970 1987-08-28 247         5   34    FALSE
974 1987-09-01 293         2   35    FALSE
977 1987-09-04 104         5   35     TRUE
980 1987-09-07 261         1   36    FALSE
981 1987-09-08 127         2   36    FALSE
982 1987-09-09 132         3   36     TRUE
983 1987-09-10 298         4   36    FALSE
984 1987-09-11 180         5   36    FALSE
987 1987-09-14 209         1   37    FALSE
988 1987-09-15 165         2   37    FALSE
989 1987-09-16 300         3   37     TRUE
991 1987-09-18 126         5   37    FALSE
994 1987-09-21 140         1   38    FALSE
995 1987-09-22 126         2   38    FALSE
997 1987-09-24 227         4   38     TRUE
998 1987-09-25 156         5   38    FALSE
> 

您可以使用以下函数来完成此操作。评论应该解释它是如何工作的(如果有任何不清楚的地方请评论)。

WedOrNext <- function(w) {
  # find all the weekdays after Tuesday and before Saturday
  iwd <- .indexwday(w)
  i <- iwd > 2 & iwd < 6

  # determine which row to return
  if (any(i)) {
    # return the first weekday after Tuesday
    w[i][1L]
  } else {
    # return NA if there are no weekdays after Tuesday
    NA
  }
}

现在您可以使用 split 将数据分成每周块,lapplyWedOrNext 函数应用于每个块,然后 do.call(rbind, ...) 将其全部放入重新走到一起。 使用您提供的数据,下面的代码演示了如果周二之后没有数据会发生什么情况。

# run on your original object
x <- structure(c(200.764, 202.502, 202.683, 204.642, 206.051, 207.702, 207.63,
  207.585, 207.843, 209.723, 212.843), .Dim = c(11L, 1L),
  index = structure(c(484185600, 484272000, 484358400, 484444800, 484531200,
  484790400, 484876800, 484963200, 485049600, 485136000, 485395200),
  tzone = "UTC", tclass = "Date"), class = c("xts", "zoo"),
  .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC")
# The last week has no data after Tuesday, so it's not represented
do.call(rbind, lapply(split(x, "weeks"), WedOrNext))
#               [,1]
# 1985-05-08 202.683
# 1985-05-15 207.585

如果周三没有数据,它将如何执行。

y <- structure(c(200.764, 202.502, 202.683, 204.642, 206.051, 207.702, 207.63,
  207.585, 207.843, 209.723, 212.843, 200, 201, 202), class = c("xts", "zoo"),
  .Dim = c(14L, 1L), index = structure(c(484185600, 484272000, 484358400,
  484444800, 484531200, 484790400, 484876800, 484963200, 485049600, 485136000,
  485395200, 485481600, 485654400, 485740800), tzone = "UTC", tclass = "Date"),
  .indexCLASS = "Date", .indexTZ = "UTC", tclass = "Date", tzone = "UTC")
do.call(rbind, lapply(split(y, "weeks"), WedOrNext))
#               [,1]
# 1985-05-08 202.683
# 1985-05-15 207.585
# 1985-05-23 201.000