Dear R-gurus:
I want to convert a zoo object of daily stock prices
to a weekly one, based on the following 4 steps.
1. No problem in creating weekly data using Wednesday Obs.
wed.index <- ( weekdays(index(daily)) == "Wed" )
week.dat <- daily[wed.index]> week.dat
A000010 A000020 A000030 A000040 A000050 A000060
2000-01-05 3925 NA 3950 6350 21717 836
2000-01-12 3500 NA 3600 6000 NA 1021
2000-01-19 2970 2701 3135 5600 21812 787
2000-01-26 2970 2616 2810 5300 20579 783
2000-02-02 NA 2833 3030 6700 21812 NA
2. If Wed observation is NA for any stock, then I want to replace
it with Thurday observation.
3. If Thurday obs is still NA, then replace it with Tuesday obs.
4. If still NA, then let it be recorded as NA.
Does anyone have some fancy idea to do steps 2-4?
Thanks in advance.
SW Kim
Professor of Economics, Chungbuk National Univ, Korea
################################################
daily <-
structure(c(3700, 3700, 3700, 3700, 4050, 3925, 3600, 3800, 3800,
3800, 3980, 3740, 3500, 3250, 3300, 3300, 3300, 3240, 3120, 2970,
3140, 3000, 3000, 3000, 3040, 2950, 2970, 3100, 3120, 3120, 3120,
3195, 3150, NA, 3140, 2710, 2710, 2710, 2710, 3116, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, 2993, 2993, 2965, 2852, 2701, 2606,
2663, 2663, 2663, 2701, 2587, 2616, 2738, 2757, 2757, 2757, 2720,
2701, 2833, 2767, 3840, 3840, 3840, 3840, 4150, 3950, 3500, 3600,
3600, 3600, 3800, 3700, 3600, NA, 3395, 3395, 3395, 3400, 3300,
3135, 3070, 2855, 2855, 2855, 2865, 2800, 2810, 2900, 2845, 2845,
2845, 2915, 3030, 3030, 3010, 4850, 4850, 4850, 4850, 5550, 6350,
6800, 6100, 6100, 6100, 6600, 6550, 6000, 5800, 5650, 5650, 5650,
5700, 5800, 5600, 5000, 5000, 5000, 5000, 5300, 5200, 5300, 5350,
5750, 5750, 5750, 6600, 6400, 6700, 6700, 23708, 23708, 23708,
23708, 22286, 21717, 22665, 21622, 21622, 21622, 21338, NA, NA,
21527, 22570, 22570, 22570, 22286, 21338, 21812, 21338, 20863,
20863, 20863, 20389, 20389, 20579, 23519, 23045, 23045, 23045,
23045, 20863, 21812, 21385, 692, NA, 692, 692, 727, 836, 809,
813, 813, 813, 934, 995, 1021, 1029, 965, 965, 965, 926, 856,
787, 839, 805, 805, 805, 792, 779, 783, 865, 943, 943, 943, 978,
NA, NA, 1034), .Dim = c(35L, 6L), .Dimnames = list(NULL, c("A000010",
"A000020", "A000030", "A000040",
"A000050", "A000060")), index = structure(c(10956,
10957, 10958, 10959, 10960, 10961, 10962, 10963, 10964, 10965,
10966, 10967, 10968, 10969, 10970, 10971, 10972, 10973, 10974,
10975, 10976, 10977, 10978, 10979, 10980, 10981, 10982, 10983,
10984, 10985, 10986, 10987, 10988, 10989, 10990), class = "Date"),
class = "zoo")
[[alternative HTML version deleted]]