Hi all, I have in my code some vectors that are not of equal size. I would like to be able for each of these vectors select 6 elements that are (almost) equally spaced. So the first one would be at (or close) to the beginning the last one at (or close) to the end and the other 4 equally spaced between first and last element. How can I do something like that on a vector of not known size? I would like to thank you in advance for your help Regards Alex [[alternative HTML version deleted]]
Hello, Alaois, if x is your vector maybe n <- length( x) positions <- trunc( quantile( seq( n), prob = 0:5/5)) x[ positions] comes close to what you want. Hth -- Gerrit> Hi all, I have in my code some vectors that are not of equal size. I > would like to be able for each of these vectors select 6 elements that > are (almost) equally spaced. So the first one would be at (or close) to > the beginning the last one at (or close) to the end and the other 4 > equally spaced between first and last element. > > How can I do something like that on a vector of not known size? > > I would like to thank you in advance for your help > > Regards > Alex > [[alternative HTML version deleted]] > > ______________________________________________ > 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.
Alaios <alaios <at> yahoo.com> writes:> I have in my code some vectors that are not of equal size.I would like to be able for each of these vectors> select 6 elements that are (almost) equally spaced.So the first one would be at (or close) to the beginning> the last one at (or close) to the end and the other 4equally spaced between first and last element.> > How can I do something like that on a vector ofnot known size?> > I would like to thank you in advancefor your help Would something like this be what you are looking for? N <- 20 set.seed(16121952) x <- runif(N) x rx <- range(x) br <- seq(rx[1], rx[2], len = 6) sapply(br, function(bx){ x[which.min(abs(x - bx))] }) [1] 0.02910779 0.22708582 0.39239718 0.52419265 0.68940262 0.86889817> > Regards > Alex-- Kenneth Knoblauch Inserm U846 Stem-cell and Brain Research Institute Department of Integrative Neurosciences 18 avenue du Doyen L?pine 69500 Bron France tel: +33 (0)4 72 91 34 77 fax: +33 (0)4 72 91 34 61 portable: +33 (0)6 84 10 64 10 http://www.sbri.fr/members/kenneth-knoblauch.html