[R] irregular sequence of events

PIKAL Petr petr.pikal at precheza.cz
Mon Feb 23 11:17:31 CET 2015


Thanks to all who responded.

I like Sarah's solution best, although it did provide only a clue. It just needed a little of tweak to finish with suitable solution. For the record I put here this tweak (it is not fully reproducible but I hope it is understandable and can help others too).

# make a plot
plot(prov$date, prov$fi>0)

# interactively identify starts and ends of some events
mimo <- identify(prov$datum, prov$fi>0)

# make x as a auxiliary variable
x <- 1:nrow(prov)

# factor of events
event <- cut(x, c(liche(mimo)-1, sude(mimo)))

# discard even events
levels(event)[levels(event)%in%sude(levels(event))]<-NA

Here I could change remaining levels to any sequence.

Functions sude and liche are for selecting even and odd values from vector.

> sude
function (x)
{
    indices <- seq(along = x)
    x[indices%%2 == 0]
}

> liche
function (x)
{
    indices <- seq(along = x)
    x[indices%%2 == 1]
}

Thanks again to all who helped me.

Best regards
Petr


> -----Original Message-----
> From: Sarah Goslee [mailto:sarah.goslee at gmail.com]
> Sent: Friday, February 20, 2015 3:42 PM
> To: PIKAL Petr
> Cc: r-help at r-project.org
> Subject: Re: [R] irregular sequence of events
>
> Hi,
>
> On Fri, Feb 20, 2015 at 9:27 AM, PIKAL Petr <petr.pikal at precheza.cz>
> wrote:
> > Dear all
> >
> > I know I am missing something obvious but after few hours of trials I
> ask for some help.
> >
> > I have some sequence of values (days)
> > x <- 1:30
> >
> > and an indication of event start and end day mimo<-c(5,10, 13,16,
> > 21,27)
>
>
> > cut(x, mimo)
>
>  [1] <NA>    <NA>    <NA>    <NA>    <NA>    (5,10]  (5,10]  (5,10]
> (5,10]
>
> [10] (5,10]  (10,13] (10,13] (10,13] (13,16] (13,16] (13,16] (16,21]
> (16,21]
>
> [19] (16,21] (16,21] (16,21] (21,27] (21,27] (21,27] (21,27] (21,27]
> (21,27]
>
> [28] <NA>    <NA>    <NA>
>
> Levels: (5,10] (10,13] (13,16] (16,21] (21,27]
>
>
> should get you started. You'll need to tweak the arguments to get
> exactly what you want,
>
>
> > or
> >
> > events <- structure(list(start = c(5, 13, 21), end = c(10, 16, 27)),
> > .Names = c("start", "end"), row.names = c(NA, -3L), class =
> > "data.frame")
> >
> > I need to get a factor indicating event
> >
> > event <- c(rep(NA, 4), rep("A1", 6), rep(NA, 2), rep("A2", 4),
> rep(NA,
> > 4), rep("A3", 7), rep(NA,3))
> > factor(event)
> >
> > In such small example I can do it manually but I have a long vector
> of dates and would like to use start and end day of events either from
> mimo vector or from events data frame.
> >
> > Is there any function which does it automagically? I know I have seen
> it before but I cannot find it now.
> >
> > Best regards
> > Petr
> >
>
> --
> Sarah Goslee
> http://www.functionaldiversity.org

________________________________
Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a jsou určeny pouze jeho adresátům.
Jestliže jste obdržel(a) tento e-mail omylem, informujte laskavě neprodleně jeho odesílatele. Obsah tohoto emailu i s přílohami a jeho kopie vymažte ze svého systému.
Nejste-li zamýšleným adresátem tohoto emailu, nejste oprávněni tento email jakkoliv užívat, rozšiřovat, kopírovat či zveřejňovat.
Odesílatel e-mailu neodpovídá za eventuální škodu způsobenou modifikacemi či zpožděním přenosu e-mailu.

V případě, že je tento e-mail součástí obchodního jednání:
- vyhrazuje si odesílatel právo ukončit kdykoliv jednání o uzavření smlouvy, a to z jakéhokoliv důvodu i bez uvedení důvodu.
- a obsahuje-li nabídku, je adresát oprávněn nabídku bezodkladně přijmout; Odesílatel tohoto e-mailu (nabídky) vylučuje přijetí nabídky ze strany příjemce s dodatkem či odchylkou.
- trvá odesílatel na tom, že příslušná smlouva je uzavřena teprve výslovným dosažením shody na všech jejích náležitostech.
- odesílatel tohoto emailu informuje, že není oprávněn uzavírat za společnost žádné smlouvy s výjimkou případů, kdy k tomu byl písemně zmocněn nebo písemně pověřen a takové pověření nebo plná moc byly adresátovi tohoto emailu případně osobě, kterou adresát zastupuje, předloženy nebo jejich existence je adresátovi či osobě jím zastoupené známá.

This e-mail and any documents attached to it may be confidential and are intended only for its intended recipients.
If you received this e-mail by mistake, please immediately inform its sender. Delete the contents of this e-mail with all attachments and its copies from your system.
If you are not the intended recipient of this e-mail, you are not authorized to use, disseminate, copy or disclose this e-mail in any manner.
The sender of this e-mail shall not be liable for any possible damage caused by modifications of the e-mail or by delay with transfer of the email.

In case that this e-mail forms part of business dealings:
- the sender reserves the right to end negotiations about entering into a contract in any time, for any reason, and without stating any reasoning.
- if the e-mail contains an offer, the recipient is entitled to immediately accept such offer; The sender of this e-mail (offer) excludes any acceptance of the offer on the part of the recipient containing any amendment or variation.
- the sender insists on that the respective contract is concluded only upon an express mutual agreement on all its aspects.
- the sender of this e-mail informs that he/she is not authorized to enter into any contracts on behalf of the company except for cases in which he/she is expressly authorized to do so in writing, and such authorization or power of attorney is submitted to the recipient or the person represented by the recipient, or the existence of such authorization is known to the recipient of the person represented by the recipient.


More information about the R-help mailing list