2014-09-20 50 views
-1

區間水平看起來像這樣我的數據如何略有下降,

CASE  DEGREE  INCOME SLAB 

1   Graduate  $1000-$2000 

2   High School $2000- $3000 

等..

爲度類別我有四個級別,但對於收入板坯我有20+的水平。這20多個級別在$ 1000的範圍內。我想把它減少到10或更少,範圍在$ 10000。例如$ 1000- $ 10000,$ 10000- $ 20000等。我怎樣才能做到這一點?任何幫助將不勝感激。

回答

2

嘗試:

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 
>