Hi, I would like to cut a vector of values in parts. Each part should have an equal number of elements. for example: x <- (rnorm(500)^2) now I want 5 vectors each with 100 elements. The first vector should include the 100 lowest values of x and so on (so that the fifth vector contains the 100 highest values of x). thanks for any help!
?sort ?cut On Tue, Feb 26, 2013 at 12:39 PM, Martin Batholdy <batholdy at googlemail.com> wrote:> Hi, > > I would like to cut a vector of values in parts. > Each part should have an equal number of elements. > > for example: > > x <- (rnorm(500)^2) > > now I want 5 vectors each with 100 elements. > The first vector should include the 100 lowest values of x and so on > (so that the fifth vector contains the 100 highest values of x). > > > thanks for any help! > > ______________________________________________ > 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.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
Hello, Try the following. x <- rnorm(500)^2 split(x, cut(x, quantile(x, probs = seq(0, 1, by = 0.2)))) Hope this helps, Rui Barradas Em 26-02-2013 17:39, Martin Batholdy escreveu:> Hi, > > I would like to cut a vector of values in parts. > Each part should have an equal number of elements. > > for example: > > x <- (rnorm(500)^2) > > now I want 5 vectors each with 100 elements. > The first vector should include the 100 lowest values of x and so on > (so that the fifth vector contains the 100 highest values of x). > > > thanks for any help! > > ______________________________________________ > 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. >
Insert "include.lowest=TRUE" or the lowest value will get dropped (assigned NA): split(x, cut(x, quantile(x, probs = seq(0, 1, by = 0.2)), include.lowest=TRUE)) ---------------------------------------------- David L Carlson Associate Professor of Anthropology Texas A&M University College Station, TX 77843-4352> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Rui Barradas > Sent: Tuesday, February 26, 2013 1:59 PM > To: Martin Batholdy > Cc: r-help at r-project.org > Subject: Re: [R] cut a vector in equal parts > > Hello, > > Try the following. > > x <- rnorm(500)^2 > split(x, cut(x, quantile(x, probs = seq(0, 1, by = 0.2)))) > > > Hope this helps, > > Rui Barradas > > Em 26-02-2013 17:39, Martin Batholdy escreveu: > > Hi, > > > > I would like to cut a vector of values in parts. > > Each part should have an equal number of elements. > > > > for example: > > > > x <- (rnorm(500)^2) > > > > now I want 5 vectors each with 100 elements. > > The first vector should include the 100 lowest values of x and so on > > (so that the fifth vector contains the 100 highest values of x). > > > > > > thanks for any help! > > > > ______________________________________________ > > 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. > > > > ______________________________________________ > 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.
There is a discussion of the ways to do this task (which can be trickier than it seems) at: http://www.portfolioprobe.com/2012/12/24/miles-of-iles/ Pat On 26/02/2013 17:39, Martin Batholdy wrote:> Hi, > > I would like to cut a vector of values in parts. > Each part should have an equal number of elements. > > for example: > > x <- (rnorm(500)^2) > > now I want 5 vectors each with 100 elements. > The first vector should include the 100 lowest values of x and so on > (so that the fifth vector contains the 100 highest values of x). > > > thanks for any help! > > ______________________________________________ > 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. >-- Patrick Burns pburns at pburns.seanet.com twitter: @burnsstat @portfolioprobe http://www.portfolioprobe.com/blog http://www.burns-stat.com (home of: 'Impatient R' 'The R Inferno' 'Tao Te Programming')