Here is a solution using the sqldf package:
> require(sqldf) > timeline <- data.frame(time = 1:30) > 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") > # add event number > events$num <- paste0("A", seq(nrow(events))) > events start end num 1 5 10 A1 2 13 16 A2 3 21 27 A3 > > sqldf(" + 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.pi...@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) > > 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. > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.