categorizing data in R

后端 未结 3 964
逝去的感伤
逝去的感伤 2021-01-24 14:32

Im trying to categorising my data into different group based on type of data. My data and code is as follow:

bank    ROE
bank1   0.73
bank2   0.94
bank3   0.62
b         


        
3条回答
  •  醉话见心
    2021-01-24 15:09

    The == operator in your code compares the vector test$bank with the vectors jov. As these vectors are of different lengths (12 and 5) and the longer vector is not a multiple of the shorter one such as in the case of sob (of length 3), you get a warning message.

    To evaluate if a value is equal to any of the values in a vector you can use the %in% operator just as @ako suggest. However when working with groups factor and levels are useful functions. Specify the variable as a factor, then set new levels.

    test <- data.frame(
      bank = c('bank1','bank2','bank3','bank4','bank5','bank6','bank7','bank8','bank9','bank10','bank11','bank12'),
      ROE = c(0.73,0.94,0.62,0.57,0.31,0.53,0.39,0.01,0.16,0.51,0.84,0.18)
    )
    
    test$bank <- factor(test$bank)
    
    levels(test$bank) <- list(
      '1' = c('bank1', 'bank2','bank3'),
      '2' = c('bank4','bank5', 'bank6'),
      '3' = c('bank7', 'bank8','bank9', 'bank10','bank11'),
      'other' = NA
    )
    
    test$bank[is.na(test$bank)] <- 'other'
    

提交回复
热议问题