[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 00:11:17 CEST 2019


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



More information about the R-help mailing list