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
Tianchan Niu <niu <at> isis.georgetown.edu> writes:> > Dear all, > I would like to use cut() to make numerics to factors, the sample codes are asfollows. However, the result is> not what I want, since r[3] =?9 should be in the interval of "8-10%" ratherthan "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, > TianchanI think you're making the problem a little harder than it needs to be ... r <- c(1,1,9,1,1,1) col_no <- cut(r,c(0,2,4,6,8,10,100), labels = c("<2%","2-4%","4-6%","6-8%","8-10%",">10%")) [1] <2% <2% 8-10% <2% <2% <2% Levels: <2% 2-4% 4-6% 6-8% 8-10% >10%
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, > TianchanIt 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] ucl.ac.uk/~ucfagls UK. WC1E 6BT. [w] freshwaters.org.uk %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
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, > TianchanBest 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 uni-giessen.de/cms/eichner ---------------------------------------------------------------------