Skip to content

irregular sequence of events

6 messages · Sarah Goslee, jim holtman, JS Huang +1 more

#
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)

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

________________________________
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.
#
Hi,
On Fri, Feb 20, 2015 at 9:27 AM, PIKAL Petr <petr.pikal at precheza.cz> wrote:
[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,

  
    
#
Here is a solution using the sqldf package:
+  "end"), row.names = c(NA, -3L), class = "data.frame")
start end num
1     5  10  A1
2    13  16  A2
3    21  27  A3
+     select t.*, e.num
+         from timeline t
+         left join (
+             select t.*, e.num
+                 from timeline t, events e
+                 where t.time between e.start and e.end) as e
+             on t.time = e.time
+ ")
   time  num
1     1 <NA>
2     2 <NA>
3     3 <NA>
4     4 <NA>
5     5   A1
6     6   A1
7     7   A1
8     8   A1
9     9   A1
10   10   A1
11   11 <NA>
12   12 <NA>
13   13   A2
14   14   A2
15   15   A2
16   16   A2
17   17 <NA>
18   18 <NA>
19   19 <NA>
20   20 <NA>
21   21   A3
22   22   A3
23   23   A3
24   24   A3
25   25   A3
26   26   A3
27   27   A3
28   28 <NA>
29   29 <NA>
30   30 <NA>
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
On Fri, Feb 20, 2015 at 9:27 AM, PIKAL Petr <petr.pikal at precheza.cz> wrote:
#
A little shorter version of the SQL solution after consulting with my
SQL expert:

 > require(sqldf)
+  "end"), row.names = c(NA, -3L), class = "data.frame")
start end num
1     5  10  A1
2    13  16  A2
3    21  27  A3
+     select t.*, e.num
+         from timeline t
+         left join events as e
+         on t.time between e.start and e.end
+ ")
   time  num
1     1 <NA>
2     2 <NA>
3     3 <NA>
4     4 <NA>
5     5   A1
6     6   A1
7     7   A1
8     8   A1
9     9   A1
10   10   A1
11   11 <NA>
12   12 <NA>
13   13   A2
14   14   A2
15   15   A2
16   16   A2
17   17 <NA>
18   18 <NA>
19   19 <NA>
20   20 <NA>
21   21   A3
22   22   A3
23   23   A3
24   24   A3
25   25   A3
26   26   A3
27   27   A3
28   28 <NA>
29   29 <NA>
30   30 <NA>
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
On Fri, Feb 20, 2015 at 9:27 AM, PIKAL Petr <petr.pikal at precheza.cz> wrote:
#
Hi,

  Herr is one implementation with function named eventList.
[1]  5 13 21
[1]  5 13 21
[1] 10 16 27
[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
25 26 27 28 29 30
function(start, end, x)
{
  result <- character(0)
  for (i in 1:length(start))
  {
    if (i == 1)
    {
      if (start[i] > 1)
      {
        result <- c(result, rep(NA, start[i] - 1))
      }
      result <- c(result, rep(paste0("A",i), end[i] - start[i] + 1))
    }
    else
    {
      if (start[i] > end[i - 1] + 1)
      {
        result <- c(result, rep(NA, start[i] - end[i - 1] - 1))
      }
      result <- c(result, rep(paste0("A", i), end[i] - start[i] + 1))
    }
  }
  if (end[length(start)] < length(x))
  {
    result <- c(result, rep(NA, length(x) - end[length(start)]))
  }
  return(result)
}
[1] NA   NA   NA   NA   "A1" "A1" "A1" "A1" "A1" "A1" NA   NA   "A2" "A2"
"A2" "A2" NA   NA   NA  
[20] NA   "A3" "A3" "A3" "A3" "A3" "A3" "A3" NA   NA   NA
--
View this message in context: http://r.789695.n4.nabble.com/irregular-sequence-of-events-tp4703579p4703624.html
Sent from the R help mailing list archive at Nabble.com.
2 days later
#
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.
function (x)
{
    indices <- seq(along = x)
    x[indices%%2 == 0]
}
function (x)
{
    indices <- seq(along = x)
    x[indices%%2 == 1]
}

Thanks again to all who helped me.

Best regards
Petr
________________________________
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.