[R] Check to see if a value is included within intervals

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Sat Jul 20 01:52:46 CEST 2019


There is no reason for you to tell anyone on this list how they should accomplish your goal... we are not a free (or paid) programming service.

If you are in fact obligated to use your  proposed approach, then you are most likely doing homework and this list is expressly not for homework (re-read the Posting Guide).

If you are doing this on your own then go now and experiment with the findInterval function that Bert suggested. We are here to offer you guidance and you need to do the learning yourself.

On July 19, 2019 6:30:59 PM CDT, Marine Regis <marine.regis using hotmail.fr> wrote:
>Thank you very much Bert for your answer. I would like reproduce the
>same results as the code below:
>
>ta <- 100
>tb <- 140
>tc <- 40
>td <- 85
>
>datF <- data.frame(t = 1:3650, e = NA)
>dat <- data.frame(a = seq(1, 3650, 365),
>                  b = seq(ta, 3650, 365),
>                  c = seq(ta + 1, 3650, 365),
>                  d = seq(ta + tb, 3650, 365),
>                  e = seq(ta + tb +1, 3650, 365),
>                  f = seq(ta + tb + tc, 3650, 365),
>                  g = seq(ta + tb + tc + 1, 3650, 365),
>                  h = seq(ta + tb + tc + td, 3650, 365))
>
>datF$e <- ifelse((datF$t %in% unlist(Map(`:`, dat$a, dat$b))), 1,
>           ifelse((datF$t %in% unlist(Map(`:`, dat$e, dat$f))), -1, 0))
>
>To reproduce these results, I would like to use a code that looks like
>the code below (i.e., without data frames and R functions):
>a <- 100
>b <- 140
>c <- 40
>d <- 85
>y1 <- a + b + c + d
>
>t1 <- seq(1, y1*10, 1)
>t2 <- t1/y1 - floor(t1/y1)
>p1 <- a/y1
>p2 <- p1 + a/y1
>p3 <- p2 + b/y1
>p4 <- p3 + c/y1
>
>test <- 1*(t2 <= p1)- 1*(t2 > p2)*(t2 <= p3)
>
>Thank you very much for your help.
>Have a nice day
>Marine
>
>
>________________________________
>De : Bert Gunter <bgunter.4567 using gmail.com>
>Envoy� : samedi 20 juillet 2019 00:46
>� : Marine Regis <marine.regis using hotmail.fr>
>Cc : r-help using r-project.org <r-help using r-project.org>
>Objet : Re: [R] Check to see if a value is included within intervals
>
>If I understand correctly (make sure that I do!), ?findInterval should
>essentially do what you want.
>
>In your example (thanks!), I assume that:
>1) The cutpoints defining your intervals are increasing, so p1 < p2 <
>p3  (p4 is unused)
>2) You want to know which t2's are in the two intervals t2 <= p1  and
>p2 < t2 <= p3.
>
>If that is correct,
>
>fi <- findInterval(t2, c(p1, p2, p3))
>
>will give you a vector of 0's, 1's, 2's, and 3's. Indices with 0's are
>those for which t2 <= p1. Indices with 2's are those for which p2 <t2
><= p3.   1's are for indices with p1 <t2 <= p2  and 3's are for those
>t2's > p3.
>
>Function parameters can control whether intervals are open or closed on
>left and/or right.
>
>Cheers,
>Bert
>
>
>On Fri, Jul 19, 2019 at 3:11 PM Marine Regis
><marine.regis using hotmail.fr<mailto:marine.regis using hotmail.fr>> wrote:
>Hello all,
>
>The R code below tests if values of the variable �t� are included or
>not within intervals that are defined from the data frame dat. The
>expected results are displayed using the function "rle" (see code
>below). Here is the code:
>
>ta <- 100
>tb <- 140
>tc <- 40
>td <- 85
>
>datF <- data.frame(t = 1:3650, e = NA)
>dat <- data.frame(a = seq(1, 3650, 365),
>                  b = seq(ta, 3650, 365),
>                  c = seq(ta + 1, 3650, 365),
>                  d = seq(ta + tb, 3650, 365),
>                  e = seq(ta + tb +1, 3650, 365),
>                  f = seq(ta + tb + tc, 3650, 365),
>                  g = seq(ta + tb + tc + 1, 3650, 365),
>                  h = seq(ta + tb + tc + td, 3650, 365))
>
>datF$e <- ifelse((datF$t %in% unlist(Map(`:`, dat$a, dat$b))), 1,
>           ifelse((datF$t %in% unlist(Map(`:`, dat$e, dat$f))), -1, 0))
>
>## Validation
>y <- rle(datF$e)
>y$lengths[y$values==1]
>y$lengths[y$values==0]
>y$lengths[y$values==-1]
>
>
>The code works but I would like to obtain the same results without
>using data frames and the function �Map�. Here is an example:
>
>a <- 100
>b <- 140
>c <- 40
>d <- 85
>y1 <- a + b + c + d
>
>t1 <- seq(1, y1*10, 1)
>t2 <- t1/y1 - floor(t1/y1)
>p1 <- a/y1
>p2 <- p1 + a/y1
>p3 <- p2 + b/y1
>p4 <- p3 + c/y1
>
>test <- 1*(t2 <= p1)- 1*(t2 > p2)*(t2 <= p3)
>
>## Validation
>y <- rle(test)
>y$lengths[y$values==1]
>y$lengths[y$values==0]
>y$lengths[y$values==-1]
>
>Using this code, the results are not correct.
>
>Any help would be greatly appreciated.
>Many thanks
>Marine
>
>
>
>
>
>
>        [[alternative HTML version deleted]]
>
>______________________________________________
>R-help using r-project.org<mailto:R-help using 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.
>
>	[[alternative HTML version deleted]]

-- 
Sent from my phone. Please excuse my brevity.



More information about the R-help mailing list