Home > other >  How to reclassify a column in r?
How to reclassify a column in r?

Time:12-03

I have this dataframe:

Name <- c("Jon", "Bill", "Maria", "Ben", "Tina")
Age <- c(-23, -41, -32, -58, -100)
df <- data.frame(Name, Age)

I want to add a column with

values between 0 and 10    0-10
values between 10 and 20    10-20
 .....
values between 90 and 100    90-100

Desired output:

       Name Age  class
      Jon  23    -20-30
     Bill  41    -40-50
    Maria  32    -30-40
      Ben  58    -50-60
     Tina  26    -20-30

CodePudding user response:

We can use cut for finding intervals (findInterval also works) and sub for text formatting

> df$class <- sub("\\((\\d ),(\\d )\\]", "\\1-\\2", cut(df$Age, seq(0,100,10)))
> df
   Name Age class
1   Jon  23 20-30
2  Bill  41 40-50
3 Maria  32 30-40
4   Ben  58 50-60
5  Tina  26 20-30

Using your updated data

> df$class <- sub("[([](-*\\d ),(-*\\d )\\]", "\\1-\\2", cut(df$Age, seq(-100,100,10), include.lowest = TRUE))
> df
   Name  Age    class
1   Jon  -23  -30--20
2  Bill  -41  -50--40
3 Maria  -32  -40--30
4   Ben  -58  -60--50
5  Tina -100 -100--90

Note you need an extra - for negative values in the end of the interval and the intervals of your desired output is in the other way around.

  •  Tags:  
  • r
  • Related