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

Marine Regis m@r|ne@reg|@ @end|ng |rom hotm@||@|r
Sat Jul 20 01:30:59 CEST 2019


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]]



More information about the R-help mailing list