Dear list, I have a strange requirement .... I have a vector, for example v<- c(0,0,0,0,1,2,4,6,8,8,8,8). I have a value,for example x<- 4.8. I would like to understand in which sub interval of v is x. In this case, v would be in the sub interval [4,6] that is in the subinterval starting from element j=7 to the element j+1=8. Can we do that with an R command? Regards -- Corrado Topi Global Climate Change & Biodiversity Indicators Area 18,Department of Biology University of York, York, YO10 5YW, UK Phone: + 44 (0) 1904 328645, E-mail: ct529 at york.ac.uk
Try this: v[findInterval(x, v) + 0:1] On Wed, Sep 30, 2009 at 5:10 PM, Corrado <ct529 at york.ac.uk> wrote:> Dear list, > > I have a strange requirement .... I have a vector, for example v<- > c(0,0,0,0,1,2,4,6,8,8,8,8). I have a value,for example x<- 4.8. > > I would like to understand in which sub interval of v is x. In this case, v > would be in the sub interval [4,6] that is in the subinterval starting from > element j=7 to the element j+1=8. > > Can we do that with an R command? > > Regards > -- > Corrado Topi > > Global Climate Change & Biodiversity Indicators > Area 18,Department of Biology > University of York, York, YO10 5YW, UK > Phone: + 44 (0) 1904 328645, E-mail: ct529 at york.ac.uk > > ______________________________________________ > R-help at r-project.org mailing list > 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Hi, assuming v is sorted, try this, v[ findInterval(x,v)+0:1 ] see ?findInterval and perhaps ?cut HTH, baptiste 2009/9/30 Corrado <ct529 at york.ac.uk>:> Dear list, > > I have a strange requirement .... I have a vector, for example v<- > c(0,0,0,0,1,2,4,6,8,8,8,8). I have a value,for example x<- 4.8. > > I would like to understand in which sub interval of v is x. In this case, v > would be in the sub interval [4,6] that is in the subinterval starting from > element j=7 to the element j+1=8. > > Can we do that with an R command? > > Regards > -- > Corrado Topi > > Global Climate Change & Biodiversity Indicators > Area 18,Department of Biology > University of York, York, YO10 5YW, UK > Phone: + 44 (0) 1904 328645, E-mail: ct529 at york.ac.uk > > ______________________________________________ > R-help at r-project.org mailing list > 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. >
On Sep 30, 2009, at 4:10 PM, Corrado wrote:> Dear list, > > I have a strange requirement .... I have a vector, for example v<- > c(0,0,0,0,1,2,4,6,8,8,8,8). I have a value,for example x<- 4.8. > > I would like to understand in which sub interval of v is x. In this > case, v > would be in the sub interval [4,6] that is in the subinterval > starting from > element j=7 to the element j+1=8.sum(x > v) > v[sum(x > v)] [1] 4 > v[sum(x > v)+1] [1] 6> > Can we do that with an R command? >-- David Winsemius, MD Heritage Laboratories West Hartford, CT
Possibly Parallel Threads
- goodness of "prediction" using a model (lm, glm, gam, brt, regression tree .... )
- Error "singular gradient matrix at initial parameter estimates" in nls
- Strange error returned or bug in gam in mgcv????
- From THE R BOOK -> Warning: In eval(expr, envir, enclos) : non-integer #successes in a binomial glm!
- Concave hull