Skip to content

converting to factor

5 messages · R. Sean Bowman, Sundar Dorai-Raj, Jerome Asselin +1 more

#
hello,

I have a list of numbers that I want to convert into factors representing
ranges of values.  For example, if I have

c(2.5, 1.6, 3.2)

I might want a list of factors like

c("<3", "<3", ">3")

I think I can do this by writing a function and using apply, as.matrix,
&c., but I'm looking for a nicer way.  Any help is greatly appreciated.

Thanks!
Sean
#
R. Sean Bowman wrote:
See ?cut.

R> cut(c(2.5, 1.6, 3.2), 3)
[1] (2.13,2.67] (1.6,2.13]  (2.67,3.2]
Levels: (1.6,2.13] (2.13,2.67] (2.67,3.2]
#
?cut

#E.g.,
cut(c(2.5, 1.6, 3.2),c(-Inf,3,Inf))
#[1] (-Inf,3] (-Inf,3] (3,Inf]

See also the "labels" option of cut().

HTH,
J.
On April 22, 2003 02:14 pm, R. Sean Bowman wrote:
#
R. Sean Bowman wrote:
Sorry, wrong syntax for what you asked for:


R>
R> cut(c(2.5, 1.6, 3.2), breaks = c(-Inf, 3, Inf))
[1] (-Inf,3] (-Inf,3] (3,Inf]
Levels: (-Inf,3] (3,Inf]
R>