[R] split variable / create categories

Ben Tupper btupper at bigelow.org
Fri Sep 9 17:39:35 CEST 2011


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



More information about the R-help mailing list