Search code examples
rclasscut

How to give a value of class after using cut function with R


I would like to change a class defined by two values in brackets by the mean value of the corresponding classes. Here is the situation: I am using "cut" to split a variable into 10 classes.

data$classe_Ta<-cut(data$Ta,10,include.lowest = TRUE)
table(data$classe_Ta)

The result gives:

    > [-12.4,-7.81] (-7.81,-3.25]  (-3.25,1.31]   (1.31,5.87]   (5.87,10.4]     (10.4,15]     (15,19.6]   (19.6,24.1]   (24.1,28.7]   (28.7,33.3] 
       59           490          2783          6028          7561          7051          5090          1400           211            15 

I would like to replace [-12.4,-7.81], (-7.81,-3.25] ...in my data frame by the mean of each class; which means by -10.105, -5.53....which needs to be calculated for each class. Any suggestions in how to do that would be much appreciated. Thanks in advance!


Solution

  • We could use str_extract to extract all the numeric elements, convert the string to numeric, get the mean.

    v1 <-  sapply(str_extract_all(levels(data$classe_Ta), 
          "-?[0-9]+(\\.[0-9]+)?"), function(x) mean(as.numeric(x)))
    
    data$Mean <- v1[as.numeric(data$classe_Ta)]
    

    data

    set.seed(24)
    data <- data.frame(Ta = sample(-50:100, 20, replace=TRUE))
    data$classe_Ta<-cut(data$Ta,10,include.lowest = TRUE)