[R] Extracting Elements By Date

Gabor Grothendieck ggrothendieck at gmail.com
Wed Jun 9 12:54:49 CEST 2010


Try this and next time provide reproducible code and data as per
posting guide (see last line of every message).

First we create a sample data frame, DF.   Then we use the zoo time
series package to read it in and convert it from long form, DF, to
wide form, z, with one column per id.

Then we use split to split it into two day groups noting that %/%
signifies integer division. Since we have 4 successive days here this
gives two groups of two days each.

See ?read.zoo and the three zoo vignettes (pdf documents) that come
with the zoo package.  Also see ?split

> # sample data
> DF <- data.frame(Date = as.Date("2000-1-1") + 0:3, id = "A", value = 1:4)
> DF <- rbind(DF, transform(DF, id = "B"))
> DF
        Date id value
1 2000-01-01  A     1
2 2000-01-02  A     2
3 2000-01-03  A     3
4 2000-01-04  A     4
5 2000-01-01  B     1
6 2000-01-02  B     2
7 2000-01-03  B     3
8 2000-01-04  B     4
>
> library(zoo)
> # need devel version of read.zoo
> source("http://r-forge.r-project.org/scm/viewvc.php/*checkout*/pkg/zoo/R/read.zoo.R?revision=719&root=zoo")
> z <- read.zoo(DF, split = "id")
> z
           A B
2000-01-01 1 1
2000-01-02 2 2
2000-01-03 3 3
2000-01-04 4 4
> split(z, as.numeric(time(z) - start(z)) %/% 2)
$`0`
           A B
2000-01-01 1 1
2000-01-02 2 2

$`1`
           A B
2000-01-03 3 3
2000-01-04 4 4


On Tue, Jun 8, 2010 at 9:49 PM, Jeff08 <jefferyding at gmail.com> wrote:
>
> Dear R Gurus,
>
> Thanks for any help in advance!
>
> Date.frame: Returns.names
>
>       X       id ticker      date_ adjClose   totret RankStk
> 258060 258060 13645T10     CP 2001-06-29   18.125 1877.758
>
> My data frame is in the above format. I would like to filter by period, per
> id (every 125 days) each consisting of 250 days, I.e. 1-250, 126-375, etc.
>
> One important thing to note is that not all ID's have the same number of
> dates.
>
> x<-unique(Returns.names$date_) gives me the ordered list of all the dates,
> so for each period i want it would be x[((n-1)*125+1):((n+1)*125)]
>
> when i tried to filter for just 1 period, it worked fine, but alas, I do not
> know how to work around the problem that you cannot assign dynamic variables
> in loops (say I named the variable for the extracted dates Returns.period,
> it would get overwritten every iteration of the loop, and I cant name
> something Returns.n, where n is the index for the loop)
>
> Thus, I tried to adjust by dynamically assigning each period to a column.
> However, I get the error: object 'Returns.period1' not found. And R is not
> like Java where you can simply declare a variable by typing say
> Returns.period1.
>
> I do not know how to create a flexible empty matrix that would allow this
> code to work.
>
> If anyone knows how to do this without a loop, that would be even better!
>
> ##Filtering by Period
> n<-1
> while(n<=19) {
>        Returns.period<-lapply(((n-1)*125+1):((n+1)*125), function (i)
> which(Returns.filter$date_==x[i]))
>        Returns.period<-unlist(Returns.period)
>
> Returns.period1[n,1:(length(Returns.period))]<-Returns.filter[Returns.period,]
>        n<-n+1
>        }
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Extracting-Elements-By-Date-tp2248227p2248227.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org 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.
>



More information about the R-help mailing list