[R] Question about cut()

Gerrit Eichner Gerrit.Eichner at math.uni-giessen.de
Tue Dec 14 17:00:35 CET 2010


Tianchan, why aren't you just using

> col_no <- cut(r,c(0,2,4,6,8,10,100))
> levels(col_no) <- c("<2%","2-4%","4-6%","6-8%","8-10%",">10%")

?

Your use of as.numeric() is nonsensical; check step by step what is
happening with that.

Hth,

Gerrit

On Tue, 14 Dec 2010, 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

  Best regards  --  Gerrit
  Best regards  --  Gerrit Eichner
  Viele Grüße  --  Gerrit
  Viele Grüße  --  Gerrit Eichner
  Viele Grüße  --  GE
  Freundliche Grüße  --  Gerrit Eichner
  Freundliche Grüße  --  GE
  Grüße  --  Gerrit
  Grüße  --  Gerrit Eichner
  Grüße  --  GE
  Gruß  --  G

---------------------------------------------------------------------
AOR Dr. Gerrit Eichner               Mathematical Institute, Room 212
gerrit.eichner at math.uni-giessen.de   Justus-Liebig-University Giessen
Tel: +49-(0)641-99-32104          Arndtstr. 2, 35392 Giessen, Germany
Fax: +49-(0)641-99-32109        http://www.uni-giessen.de/cms/eichner
---------------------------------------------------------------------


More information about the R-help mailing list