Perhaps this untested stab at an answer. Tested solutions can be provided if you first provide a data object. There's also ambiguity about what an "equal category" might be. Equal counts? Or equal span? This answers the equal span which is what cut
would deliver.
nost13$actclass2 <- ifelse(nost13$actsum > 0,
cut(nost13$actsum ,6), 0)
I suspect the coercion to numeric will occur inside ifelse
. Your code would have attempted to append 0's to factors which would have ended in tears. If you wnat this to be a factor with levels "0"-"6" then wrap the entire ifelse(....)
in factor(.)
.
Here's some lightweight testing:
actclass2 <- ifelse(-100:100 > 0,
cut(-100:100 ,6), 0)
table(actclass2)
#------------
actclass2
0 4 5 6
101 33 33 34
So depending on the distribution of values, you might not have gotten exactly what you wanted. This shows a modification of that strategy that will probably be more pleasing:
> vals <- -100:100
> splits <- seq(min(vals[vals>0]),max(vals[vals>0]), length=8)[-8]
> actclass2 <- ifelse(vals > 0,
+ cut(vals ,breaks=splits ), 0)
> table(actclass2)
actclass2
0 1 2 3 4 5 6
101 14 14 14 14 14 14
Need a sequence of length = 8 to get 6 intervals with cut, since the max value was discarded and need 7 boundaries to generate 6 intervals. After going through this I'm thinking that the findInterval function would produce a clearer path to success.
> table( findInterval( vals, c(-Inf, 0, splits[-1], Inf) ))
1 2 3 4 5 6 7 8
100 16 14 14 14 14 14 15
findInterval
has intervals closed on the left versus cut
whose default is closed on the right.