[R] Help with Dates
jim holtman
jholtman at gmail.com
Fri Jul 20 01:36:56 CEST 2007
Try some of the following:
head(subset(df, Yr %in% c("00","01","02","03")))
subset(df, (Yr >= '00') & (Yr <= '03')) # same as above
subset(df, (Yr == '00') | (Yr == '01') | (Yr == '02') |(Yr == '03')) # same
On 7/19/07, Alex Park <alex.park1 at ntlworld.com> wrote:
> R
>
> I am taking an excel dataset and reading it into R using read.table.
> (actually I am dumping the data into a .txt file first and then reading data
> in to R).
>
> Here is snippet:
>
> > head(data);
> Date Price Open.Int. Comm.Long Comm.Short net.comm
> 1 15-Jan-86 673.25 175645 65910 28425 37485
> 2 31-Jan-86 677.00 167350 54060 27120 26940
> 3 14-Feb-86 680.25 157985 37955 25425 12530
> 4 28-Feb-86 691.75 162775 49760 16030 33730
> 5 14-Mar-86 706.50 163495 54120 27995 26125
> 6 31-Mar-86 709.75 164120 54715 30390 24325
>
> The dataset runs from 1986 to 2007.
>
> I want to be able to take subsets of my data based on date e.g. data between
> 2000 - 2005.
>
> As it stands, I can't work with the dates as they are not in correct format.
>
> I tried successfully converting the dates to just the year using:
>
> transform(data, Yr = format(as.Date(as.character(Date),format = '%d-%b-%y'),
> "%y")))
>
> This gives the following format:
>
> Date Price Open.Int. Comm.Long Comm.Short net.comm Yr
> 1 15-Jan-86 673.25 175645 65910 28425 37485 86
> 2 31-Jan-86 677.00 167350 54060 27120 26940 86
> 3 14-Feb-86 680.25 157985 37955 25425 12530 86
> 4 28-Feb-86 691.75 162775 49760 16030 33730 86
> 5 14-Mar-86 706.50 163495 54120 27995 26125 86
> 6 31-Mar-86 709.75 164120 54715 30390 24325 86
>
> I can subset for a single year e.g:
>
> head(subset(df, Yr =="00")
>
> But how can I subset for multiple periods e.g 00- 05? The following won't
> work:
>
> head(subset(df, Yr =="00" & Yr=="01")
>
> or
>
> head(subset(df, Yr = c("00","01","02","03")
>
> I can't help but feeling that I am missing something and there is a simpler
> route.
>
> I leafed through R newletter 4.1 which deals with dates and times but it
> seemed that strptime and POSIXct / POSIXlt are not what I need either.
>
> Can anybody help me?
>
> Regards
>
>
> Alex
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
More information about the R-help
mailing list