[R] split variable / create categories

David L Carlson dcarlson at tamu.edu
Fri Sep 9 18:50:42 CEST 2011


This will work as long as the number of values is evenly divisible by four
and no two observations are identical. But if either of those assumptions is
false, you cannot be certain to get the even split you are looking for

x <- c(3.2,  1.5,  6.8,  6.9,  8.5,  9.6,  1.1,  0.6)

ceiling(rank(x)/(length(x)/4))

[1] 2 2 3 3 4 4 1 1

----------------------------------------------
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 Jean-Christophe BOUËTTÉ
Sent: Friday, September 09, 2011 11:21 AM
To: Martin Batholdy
Cc: R Help
Subject: Re: [R] split variable / create categories

so what about :
library(ggplot2)
z <- breaks(x,equal=NULL,nbins=4)
a <- cut(x,z,include.lowest = TRUE)
it works for me !
JC

2011/9/9 Martin Batholdy <batholdy at googlemail.com>:
> 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 at 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 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.
>>
>>
>>
>> --
>> Andrea Spano'
>>
>> Quantide s.r.l
>> +39 347 747 04 92.
>> andrea.spano at 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]]
>
>
> ______________________________________________
> 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.



More information about the R-help mailing list