[R] Assigning value Range for values

arun smartpink111 at yahoo.com
Tue May 27 14:52:13 CEST 2014


Hi Praveen,
Try this:
dat <- read.table(text="INCOME
 20100
 26800
 50000
 180000",sep="",header=TRUE)

transform(dat, INCOME_RANGE=as.character(cut(INCOME,breaks=c(0,25,50,100,150,180,Inf)*1e3,right=FALSE,labels=c("below 25,000", "25,000-49,999.99", "50,000-99,999.99", "1,00,000-1,49,999.99", "1,50,000-1,79,999.99", "above 1,80,000"))))
# INCOME     INCOME_RANGE
#1  20100     below 25,000
#2  26800 25,000-49,999.99
#3  50000 50,000-99,999.99
#4 180000   above 1,80,000


A.K.


 




  Hi Arun,

Thanks for the reply.
But this code suits only when we have the range differences as 25000. But Mine is different

I need as
below 25000
'25,000-49,999.99'
'50,000-99,999.99'
'1,00,000-1,49,999.99'
'1,50,000-1,79,999.99'
'above 1,80,000'

Can you help me with this ?


Thanks in advance.
Praveen 


On Monday, May 26, 2014 1:12 PM, arun <smartpink111 at yahoo.com> wrote:
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