Hi, is there a function or an easy way to convert a variable with continuous values into a categorial variable (with x levels)? here is what I mean: I want to transform x: x <- c(3.2, 1.5, 6.8, 6.9, 8.5, 9.6, 1.1, 0.6) into a 'categorial'-variable with four levels so that I get: [1] 2 2 3 3 4 4 1 1 so each element is converted into its rank- value / categorial-value (in this example four levels are created). thanks for any suggestions!
Hi, On Sep 9, 2011, at 11:34 AM, Martin Batholdy wrote:> Hi, > > is there a function or an easy way to convert a variable with continuous values into a categorial variable (with x levels)? > > here is what I mean: > > > I want to transform x: > > x <- c(3.2, 1.5, 6.8, 6.9, 8.5, 9.6, 1.1, 0.6) > > into a 'categorial'-variable with four levels so that I get: > > [1] 2 2 3 3 4 4 1 1 > > so each element is converted into its rank- value / categorial-value > (in this example four levels are created). > >Try findInterval()> x <- c(3.2, 1.5, 6.8, 6.9, 8.5, 9.6, 1.1, 0.6) > findInterval(x, c(0, 1.5, 6, 8))[1] 2 2 3 3 4 4 1 1 CHeers, Ben> > thanks for any suggestions! > > ______________________________________________ > 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.Ben Tupper Bigelow Laboratory for Ocean Sciences 180 McKown Point Rd. P.O. Box 475 West Boothbay Harbor, Maine 04575-0475 http://www.bigelow.org
Hi there, library(lattice) equal.count(x,number=4,overlap=0) JC 2011/9/9 Martin Batholdy <batholdy at googlemail.com>:> Hi, > > is there a function or an easy way to convert a variable with continuous values into a categorial variable (with x levels)? > > here is what I mean: > > > I want to transform x: > > x <- c(3.2, ?1.5, ?6.8, ?6.9, ?8.5, ?9.6, ?1.1, ?0.6) > > into a 'categorial'-variable with four levels so that I get: > > [1] 2 2 3 3 4 4 1 1 > > so each element is converted into its rank- value / categorial-value > (in this example four levels are created). > > > > thanks for any suggestions! > > ______________________________________________ > 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. >
Thanks for the suggestions! However all these functions don't produce exactly what I want (at least with my actual data). I need a split-algorithm that converts the values of my vectors into four factors. And the crucial part is, that I need exactly the same number of elements in each factor-level and no overlapping. cut() seems to find equal intervals – but that leads to different numbers of elements in each interval. library(lattice) equal.count(x,number=4,overlap=0) seems to do the job, but strangely enough, it seems to ignore the argument 'overlap = 0' in my actual vector – I get factor-borders that overlap. And I really have to prevent this. On 09.09.2011, at 17:49, Andrea Spano wrote:> cut ( x , c(0, 1.4 ,6, 8, Inf ), labels = 1:4, include.lowest = T) > > On 9 September 2011 17:34, Martin Batholdy <batholdy@googlemail.com> wrote: > Hi, > > is there a function or an easy way to convert a variable with continuous values into a categorial variable (with x levels)? > > here is what I mean: > > > I want to transform x: > > x <- c(3.2, 1.5, 6.8, 6.9, 8.5, 9.6, 1.1, 0.6) > > into a 'categorial'-variable with four levels so that I get: > > [1] 2 2 3 3 4 4 1 1 > > so each element is converted into its rank- value / categorial-value > (in this example four levels are created). > > > > thanks for any suggestions! > > ______________________________________________ > R-help@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. > > > > -- > Andrea Spano' > > Quantide s.r.l > +39 347 747 04 92. > andrea.spano@quantide.com > > > This electronic mail transmission may contain confidential information > addressed only to the person (s) named. Any use, distribution, copyng or > disclosure by any other person and/or entities other than the intended > recipient is prohibited. If you received this transmission in error, > please inform the sender immediately and delete the material. >[[alternative HTML version deleted]]