[R] Assigning value Range for values

arun smartpink111 at yahoo.com
Mon May 26 19:12:11 CEST 2014


Hi,

May be you can try ?cut

dat <- read.table(text="INCOME
 20100
 26800
 55050
 82000",sep="",header=TRUE) 
dat <- transform(dat, INCOME_RANGE=as.character(cut(INCOME,breaks=seq(0,1e5, by=25e3),labels=c("below 25000", "25000-49999", "50000-74999","75000-99999"))))

dat
#  INCOME INCOME_RANGE
#1  20100  below 25000
#2  26800  25000-49999
#3  55050  50000-74999
#4  82000  75000-99999
 
BTW, the range in your loop code is different than the one you showed in the example.  

A.K.



Hi All,

I have a column called "Income".
Currently I have values as below
INCOME
 20100
 26800
 55050
 82000
I need to change this buy assigning Bucket Ranges. My output should be as below.
INCOME
below 25000
25000 - 49999
50000 - 75000
75000 - 100000

I wrote a code as below. Where it is not giving the correct answer.


for(j in 1:nrow(tablename))
{
  if (tablename_INCOME[j] >=0 & tablename_INCOME[j] < 25000){
    tablename_INCOME[j] <- 'below 25000'
  }else{
  if (tablename_INCOME[j] >= 25000 & tablename_INCOME[j] < 50000){
    tablename_INCOME[j] <- '25,000-49,999.99'
  }else{
  if (tablename_INCOME[j] >= 50000 & tablename_INCOME[j] < 100000){
    tablename_INCOME[j] <- '50,000-99,999.99'
  }else{
  if (tablename_INCOME[j] >= 100000 & tablename_INCOME[j] < 150000){
    tablename_INCOME[j] <- '1,00,000-1,49,999.99'
  }else{
  if (tablename_INCOME[j] >= 100000 & tablename_INCOME[j] < 150000){
    tablename_INCOME[j] <- '1,50,000-1,79,999.99'
  }else{
  if (tablename_INCOME[j] >= 180000){
    tablename_INCOME[j] <- 'above 1,80,000'



Can anyone help me with this ?

Regards,
Praveen 




More information about the R-help mailing list