Re: [R] Odp: how to sample a block

From: Petr PIKAL <petr.pikal_at_precheza.cz>
Date: Mon, 10 Nov 2008 14:09:46 +0100

Peter Dalgaard <p.dalgaard_at_biostat.ku.dk> napsal dne 10.11.2008 14:05:41:

> Petr PIKAL wrote:
> > Hi
> >
> > r-help-bounces_at_r-project.org napsal dne 10.11.2008 13:25:41:
> >
> >> Hi,
> >> I want to sample a block of information.
> >>
> >> Let's say x is a time series. Using sample(x,5) I get a random sample
of
> >> length 5 from x.
> >>
> >> Is it possible to sample consecutive observations, i.e. I sample one
> >> observation and also get the next 4 observations?
> >
> > Easy
> >
> > sam <- sample(x,1)
> > x[sam:(sam+4)]
>
>
> You mean
>
> sam <- sample(seq_along(x),1)
> x[sam:(sam+4)]

Yes, I meant it. Without an example I set my x series as 1:100 so it was in seq order and no need to use seq_along.

Petr

>
> I think this answers the question as posed, but possibly not as meant.
>
> You might have
>
> block <- seq_along(x) %/% 5
>
> and want to an entire block. This is a little more tricky:
>
> bl <- sample(unique(block),1)
> x[block==bl]
>
> It gets even trickier if you want to vectorize sampling of multiple
> blocks, but let's leave that for now.
>
> > Regards
> > Petr
> >
> >
> >
> >> Thanks a lot!
> >>
> >> Martin
> >> --
> >> View this message in context:
> > http://www.nabble.com/how-to-sample-a-block-
> >> tp20418997p20418997.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.
> >
> > ______________________________________________
> > 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.
>
>
> --
> O__ ---- Peter Dalgaard ุster Farimagsgade 5, Entr.B
> c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
> (*) \(*) -- University of Copenhagen Denmark Ph: (+45)
35327918
> ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX: (+45) 35327907



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. Received on Mon 10 Nov 2008 - 13:12:56 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 10 Nov 2008 - 15:30:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive