Search code examples
rcbindrecode

cbind new column based on values


My data is:

 try<-data.frame(Gender=c("Male","Male","Female","Male","Female","Male","Female","Female","Male","Male", "Female","Male","Male","Male", "Male"), 
            Est=c(0.9956, 1.035, 2.0731, 0.0824, 3.0987, 5.0982, 6.0707, 5.0393, 2.7046, 4.0783, 0, 2.0923, 4.2348, 1.9561, 6.9262))

I need to add one more column to recode the Est value into this:

    Gender    Est    X
 1    Male 0.9956    0
 2    Male 1.0350    1
 3  Female 2.0731    2
 4    Male 0.9824    0
 5  Female 3.0987    3
 6    Male 5.0982    5
 7  Female 6.0707    6
 8  Female 5.0393    5
 9    Male 2.7046    2
 10   Male 4.0783    4
 11 Female 0.0000    0
 12   Male 2.0923    2
 13   Male 4.2348    4
 14   Male 1.9561    1
 15   Male 6.9262    6

Where (0,1]=0, (1,2]=1, (2,3]=2, (3,4]=3, (4,5]=4, (5,6]=5, (6,7]=6.


Solution

  • cut divides the range of x into intervals and codes the values in x according to which interval they fall.

    As @alistaire mentioned the output of cut is a factor. So you might want to convert it back to numeric using as.numeric(as.character(cut(try$X ....)))

    try$X = cut(x = try$Est,breaks = c(0,1,2,3,4,5,6,7),labels = c(0,1,2,3,4,5,6),right = TRUE,include.lowest = TRUE)