Gavin Simpson gavin.simpson at ucl.ac.uk
Tue Dec 14 16:56:41 CET 2010

```On Tue, 2010-12-14 at 15:40 +0000, Tianchan Niu wrote:
> Dear all,
> I would like to use cut() to make numerics to factors, the sample codes are as follows. However, the result is not what I want, since r[3] = 9 should be in the interval of "8-10%" rather than "2-4%". Maybe cut() is not the right function to use for my situation. Please help. > r <- c(1,1,9,1,1,1)
> > col_no <- as.factor(as.numeric(cut(r,c(0,2,4,6,8,10,100))))
> > levels(col_no) <- c("<2%","2-4%","4-6%","6-8%","8-10%",">10%")
> > col_no
> [1] <2%  <2%  2-4% <2%  <2%  <2%
> Levels: <2% 2-4% 4-6% 6-8% 8-10% >10%Thanks,
> Tianchan

It would really help if you looked at the intermediary steps of your
calculations to see what went wrong.

The problem is here:

> as.factor(as.numeric(cut(r,c(0,2,4,6,8,10,100))))
[1] 1 1 5 1 1 1
Levels: 1 5

There are 2 levels in the data passed to as.factor, 1 and 5.
levels(...)[1] is 1, and levels(...)[2] is 5. You then assign the values
<2% 2-4% to these two levels. Hence the result

One option is to use factor and specify the levels as 1:6:

> factor(as.numeric(cut(r,c(0,2,4,6,8,10,100))), levels = 1:6)
[1] 1 1 5 1 1 1
Levels: 1 2 3 4 5 6

Using this we have:

> col_no <- factor(as.numeric(cut(r,c(0,2,4,6,8,10,100))), levels = 1:6)
> levels(col_no) <- c("<2%","2-4%","4-6%","6-8%","8-10%",">10%")
> col_no
[1] <2%   <2%   8-10% <2%   <2%   <2%
Levels: <2% 2-4% 4-6% 6-8% 8-10% >10%

I'm sure there are other, possibly better, ways of doing this, but that
is one.

HTH

G
--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

```