How to decrease the interval levels in R -
my data looks
case degree income slab 1 graduate $1000-$2000 2 high school $2000- $3000
so on..
for degree category have 4 levels income slab have 20+ levels. these 20+ levels in range of $1000. want decrease 10 or less range of $10000. $1000-$10000, $10000-$20000 on. how can that? appreciated.
try:
ori = c('$1000-$2000','$3000-$4000','$5000-$6000','$7000-$8000','$9000-$10000') > ddf = data.frame(ori) > ddf ori 1 $1000-$2000 2 $3000-$4000 3 $5000-$6000 4 $7000-$8000 5 $9000-$10000 > > ddf$ori = gsub('$','',ddf$ori, fixed=t) > ddf ori 1 1000-2000 2 3000-4000 3 5000-6000 4 7000-8000 5 9000-10000 ddf$min = lapply(strsplit(ori, '-'), function(x)x[1]) > ddf ori min 1 1000-2000 1000 2 3000-4000 3000 3 5000-6000 5000 4 7000-8000 7000 5 9000-10000 9000 > > ddf$new = ifelse(ddf$min<5000, '$1000-$5000', ifelse(ddf$min<10000,'$5000-$10000','$10000-$15000' )) > ddf ori min new 1 1000-2000 1000 $1000-$5000 2 3000-4000 3000 $1000-$5000 3 5000-6000 5000 $5000-$10000 4 7000-8000 7000 $5000-$10000 5 9000-10000 9000 $5000-$10000 >
Comments
Post a Comment