# Check to see if a value is included within intervals Classic List Threaded 9 messages Reply | Threaded
Open this post in threaded view
|

## Check to see if a value is included within intervals

 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]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: 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 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 <[hidden email]> 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]] > > ______________________________________________ > [hidden email] 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]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: Check to see if a value is included within intervals

 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 <[hidden email]> Envoy� : samedi 20 juillet 2019 00:46 � : Marine Regis <[hidden email]> Cc : [hidden email] <[hidden email]> 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 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 <[hidden email]> 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]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.         [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: Check to see if a value is included within intervals

 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 <[hidden email]> 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 <[hidden email]> >Envoy� : samedi 20 juillet 2019 00:46 >� : Marine Regis <[hidden email]> >Cc : [hidden email] <[hidden email]> >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 <= p3.   1's are for indices with p1 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 ><[hidden email]> 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]] > >______________________________________________ >[hidden email] 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. ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Connect to Oracle database via ODBC

 Dear All, can anyone point me towards information for connecting to a Oracle instance via DSN. I have already established a ODBC connection. Sorry if this is very elementary-I am just getting started with R after using SAS for almost 2 decades. Kind regards Ahson ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: Connect to Oracle database via ODBC

 > On Aug 5, 2019, at 7:03 AM, e-mail ma015k3113 via R-help <[hidden email]> wrote: > > Dear All, can anyone point me towards information for connecting to a Oracle instance via DSN. I have already established a ODBC connection. > > Sorry if this is very elementary-I am just getting started with R after using SAS for almost 2 decades. > > Kind regards > > Ahson Hi, Look at the RODBC package on CRAN by Prof. Ripley:   https://cran.r-project.org/web/packages/RODBC/index.htmland be sure to read the package vignette for additional information:   https://cran.r-project.org/web/packages/RODBC/vignettes/RODBC.pdfThere is also the r-sig-db e-mail list, which is focused in this domain, and where follow up questions should be posted:   https://stat.ethz.ch/mailman/listinfo/r-sig-dbRegards, Marc Schwartz ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: Connect to Oracle database via ODBC

 Hi Ahson, Many people use R via RStudio, an IDE that has both free and non-free versions. RStudio has invested a lot of effort into making it easier to establish connections to databases. If this sounds of interest to you, take a look at https://db.rstudio.com/HTH, Eric On Mon, Aug 5, 2019 at 2:54 PM Marc Schwartz via R-help < [hidden email]> wrote: > > > On Aug 5, 2019, at 7:03 AM, e-mail ma015k3113 via R-help < > [hidden email]> wrote: > > > > Dear All, can anyone point me towards information for connecting to a > Oracle instance via DSN. I have already established a ODBC connection. > > > > Sorry if this is very elementary-I am just getting started with R after > using SAS for almost 2 decades. > > > > Kind regards > > > > Ahson > > > Hi, > > Look at the RODBC package on CRAN by Prof. Ripley: > >   https://cran.r-project.org/web/packages/RODBC/index.html> > and be sure to read the package vignette for additional information: > >   https://cran.r-project.org/web/packages/RODBC/vignettes/RODBC.pdf> > There is also the r-sig-db e-mail list, which is focused in this domain, > and where follow up questions should be posted: > >   https://stat.ethz.ch/mailman/listinfo/r-sig-db> > Regards, > > Marc Schwartz > > ______________________________________________ > [hidden email] 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]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: Connect to Oracle database via ODBC

 In reply to this post by R help mailing list-2 Dear Marc, Thanks-much appreciated Kind regards Ahson > On 05 August 2019 at 12:54 Marc Schwartz <[hidden email]> wrote: > > > > > On Aug 5, 2019, at 7:03 AM, e-mail ma015k3113 via R-help <[hidden email]> wrote: > > > > Dear All, can anyone point me towards information for connecting to a Oracle instance via DSN. I have already established a ODBC connection. > > > > Sorry if this is very elementary-I am just getting started with R after using SAS for almost 2 decades. > > > > Kind regards > > > > Ahson > > > Hi, > > Look at the RODBC package on CRAN by Prof. Ripley: > >   https://cran.r-project.org/web/packages/RODBC/index.html> > and be sure to read the package vignette for additional information: > >   https://cran.r-project.org/web/packages/RODBC/vignettes/RODBC.pdf> > There is also the r-sig-db e-mail list, which is focused in this domain, and where follow up questions should be posted: > >   https://stat.ethz.ch/mailman/listinfo/r-sig-db> > Regards, > > Marc Schwartz > ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

## Re: Connect to Oracle database via ODBC

 In reply to this post by Eric Berger Dear Eric, thanks-much appreciated. Kind regards Ahson > On 05 August 2019 at 13:19 Eric Berger <[hidden email]> wrote: > >     Hi Ahson, >     Many people use R via RStudio, an IDE that has both free and non-free versions. >     RStudio has invested a lot of effort into making it easier to establish connections to databases. >     If this sounds of interest to you, take a look at >     https://db.rstudio.com/  > >     HTH, >     Eric >       > >     On Mon, Aug 5, 2019 at 2:54 PM Marc Schwartz via R-help < [hidden email] mailto:[hidden email] > wrote: > >         > >         > On Aug 5, 2019, at 7:03 AM, e-mail ma015k3113 via R-help < [hidden email] mailto:[hidden email] > wrote: > >         > > >         > Dear All, can anyone point me towards information for connecting to a Oracle instance via DSN. I have already established a ODBC connection. > >         > > >         > Sorry if this is very elementary-I am just getting started with R after using SAS for almost 2 decades. > >         > > >         > Kind regards > >         > > >         > Ahson > > > > > >         Hi, > > > >         Look at the RODBC package on CRAN by Prof. Ripley: > > > >           https://cran.r-project.org/web/packages/RODBC/index.html> > > >         and be sure to read the package vignette for additional information: > > > >           https://cran.r-project.org/web/packages/RODBC/vignettes/RODBC.pdf> > > >         There is also the r-sig-db e-mail list, which is focused in this domain, and where follow up questions should be posted: > > > >           https://stat.ethz.ch/mailman/listinfo/r-sig-db> > > >         Regards, > > > >         Marc Schwartz > > > >         ______________________________________________ > >         [hidden email] mailto:[hidden email] 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]] ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.