Dear R-users, a very easy one for you, I guess. I need to extract the indexes of the elements corresponding to different quantiles of a vector. When a quantile is an interpolation between two adjacent values, I need the index of the value which is closer (the lower value - or the higher value for what matters - in case the quantile is exactly half way through). This is an example.> x <- c(9,9,1,3,2,7,6,10,5,6) > quantile(x)0% 25% 50% 75% 100% 1.0 3.5 6.0 8.5 10.0 What I need is a vector 'index' which looks like> index3 4 7 1 8 Many thanks for your help ! Matteo
Here's how to get one: x <- c(9,9,1,3,2,7,6,10,5,6)> which.min(abs(x - quantile(x, .25)))[1] 4 And here's one of the various ways to get the entire set:> xq <- quantile(x) > sapply(xq, function(y)which.min(abs(x - y)))0% 25% 50% 75% 100% 3 4 7 1 8 Sarah On Tue, Nov 22, 2016 at 7:21 AM, Matteo Richiardi <matteo.richiardi at gmail.com> wrote:> Dear R-users, > a very easy one for you, I guess. I need to extract the indexes of the > elements corresponding to different quantiles of a vector. When a > quantile is an interpolation between two adjacent values, I need the > index of the value which is closer (the lower value - or the higher > value for what matters - in case the quantile is exactly half way > through). > > This is an example. > >> x <- c(9,9,1,3,2,7,6,10,5,6) >> quantile(x) > 0% 25% 50% 75% 100% > 1.0 3.5 6.0 8.5 10.0 > > What I need is a vector 'index' which looks like > >> index > 3 4 7 1 8 > > Many thanks for your help ! Matteo >-- Sarah Goslee http://www.functionaldiversity.org
> On Nov 22, 2016, at 4:21 AM, Matteo Richiardi <matteo.richiardi at gmail.com> wrote: > > Dear R-users, > a very easy one for you, I guess. I need to extract the indexes of the > elements corresponding to different quantiles of a vector. When a > quantile is an interpolation between two adjacent values, I need the > index of the value which is closer (the lower value - or the higher > value for what matters - in case the quantile is exactly half way > through). > > This is an example. > >> x <- c(9,9,1,3,2,7,6,10,5,6) >> quantile(x) > 0% 25% 50% 75% 100% > 1.0 3.5 6.0 8.5 10.0 > > What I need is a vector 'index' which looks like > >> index > 3 4 7 1 8 >Try this (calculate distances with `outer` and then sweep the columns with `apply`)> dqt <- outer(x, quantile(x), function(x,y) sqrt( (x-y)^2) ) > apply(dqt, 2, which.min)0% 25% 50% 75% 100% 3 4 7 1 8 -- David.> Many thanks for your help ! Matteo > > ______________________________________________ > R-help at 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.David Winsemius Alameda, CA, USA
You might try something like x <- c(9,9,1,3,2,7,6,10,5,6) p <- (0:4)/4 order(x) [ quantile(seq_along(x),p, type=1) ] # [1] 3 4 7 1 8 Selecting which value of 'type' works makes my head hurt. You could also use 1+(p*(length(x)-1) as the index into order(x). Bill Dunlap TIBCO Software wdunlap tibco.com On Tue, Nov 22, 2016 at 4:21 AM, Matteo Richiardi < matteo.richiardi at gmail.com> wrote:> Dear R-users, > a very easy one for you, I guess. I need to extract the indexes of the > elements corresponding to different quantiles of a vector. When a > quantile is an interpolation between two adjacent values, I need the > index of the value which is closer (the lower value - or the higher > value for what matters - in case the quantile is exactly half way > through). > > This is an example. > > > x <- c(9,9,1,3,2,7,6,10,5,6) > > quantile(x) > 0% 25% 50% 75% 100% > 1.0 3.5 6.0 8.5 10.0 > > What I need is a vector 'index' which looks like > > > index > 3 4 7 1 8 > > Many thanks for your help ! Matteo > > ______________________________________________ > R-help at 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]]
Thanks Sarah (and all the others who replied) for your precious suggestions! Matteo On 22 November 2016 at 14:18, Sarah Goslee <sarah.goslee at gmail.com> wrote:> Here's how to get one: > > x <- c(9,9,1,3,2,7,6,10,5,6) >> which.min(abs(x - quantile(x, .25))) > [1] 4 > > And here's one of the various ways to get the entire set: > >> xq <- quantile(x) >> sapply(xq, function(y)which.min(abs(x - y))) > 0% 25% 50% 75% 100% > 3 4 7 1 8 > > Sarah > > On Tue, Nov 22, 2016 at 7:21 AM, Matteo Richiardi > <matteo.richiardi at gmail.com> wrote: >> Dear R-users, >> a very easy one for you, I guess. I need to extract the indexes of the >> elements corresponding to different quantiles of a vector. When a >> quantile is an interpolation between two adjacent values, I need the >> index of the value which is closer (the lower value - or the higher >> value for what matters - in case the quantile is exactly half way >> through). >> >> This is an example. >> >>> x <- c(9,9,1,3,2,7,6,10,5,6) >>> quantile(x) >> 0% 25% 50% 75% 100% >> 1.0 3.5 6.0 8.5 10.0 >> >> What I need is a vector 'index' which looks like >> >>> index >> 3 4 7 1 8 >> >> Many thanks for your help ! Matteo >> > -- > Sarah Goslee > http://www.functionaldiversity.org