[R-SIG-Mac] R-SIG-Mac Digest, Vol 93, Issue 20

Denis Chabot chabotd at globetrotter.net
Sun Nov 28 17:26:35 CET 2010


Hi,

Not sure if this is the most elegant solution, but here it is.

You do not tell us the date format you use (chron, as.Date, other?). Regardless, you should be able to create a vector containing the sequence of the dates for a given year. Make it a data.frame.

If you use the Date class, it is easy:

y2003 = data.frame(DATE=seq(as.Date("2003-01-01"), as.Date("2003-12-31"), by=1))

then I'd do a merge:

alldays = merge(yourData, y2003, by="DATE", all.x=T, all.y=T)

Denis Chabot
Le 2010-11-25 à 06:00, r-sig-mac-request at stat.math.ethz.ch a écrit :

> Message: 3
> Date: Wed, 24 Nov 2010 19:02:17 -0500
> From: Jagdish Gangolly <gangolly at csc.albany.edu>
> To: r-sig-mac at stat.math.ethz.ch
> Subject: [R-SIG-Mac] Inserting rows.
> Message-ID: <50B374CA-2883-4D89-A74F-DEBADCECC32D at csc.albany.edu>
> Content-Type: text/plain; charset=US-ASCII; format=flowed
> 
> I have a stock price dataset  a snippet of which is:
> 
>> plcm60[1:15, c(1,3,4,5,6,7)]
>       DATE  BIDLO ASKHI    PRC     VOL       RET
> 1   1/2/03  9.450  9.79  9.700 1531819  0.018907
> 2   1/3/03  9.670  9.94  9.940 1582192  0.024742
> 3   1/6/03  9.830 10.05  9.960 1843298  0.002012
> 4   1/7/03  9.835 10.38 10.350 1412441  0.039157
> 5   1/8/03 10.220 10.67 10.260  961400 -0.008696
> 6   1/9/03 10.280 11.05 11.020 1742989  0.074074
> 7  1/10/03 10.900 11.29 11.100  701203  0.007260
> 8  1/13/03 11.150 11.44 11.350  959718  0.022523
> 9  1/14/03 11.130 11.40 11.230  994991 -0.010573
> 10 1/15/03 11.110 11.73 11.410 1678751  0.016029
> 11 1/16/03 10.850 11.40 11.079  887901 -0.029010
> 12 1/17/03 10.560 10.91 10.710  968015 -0.033306
> 13 1/21/03 10.430 10.76 10.680  747969 -0.002801
> 14 1/22/03 10.510 10.82 10.560 2702355 -0.011236
> 15 1/23/03 10.700 11.95 11.910 2262855  0.127841
> 
> Since the market is closed saturdays, sundays and holidays,
> the dataset has gaps. I need to insert rows (with dates
> but all other columns blank) so that the date
> column in the dataset will have all dates on the calendar?
> 
> Is there a simple solution using only strptime, and for loops?
> 
> 
> 
> Jagdish
> --
> Jagdish Gangolly (gangolly at gmail.com)



More information about the R-SIG-Mac mailing list