samedi 1 mai 2021

Coding a new variable in R based on a field values of both categorical and numerical data

So sorry to bother you but I am trying to classify a new variable named CODE based on the values of a field named time, this comes from a machine that records how much time in hours a pipe is under control this is the data

df<- tibble::tribble(
~time,
"5",
"4",
"NO_TIME",
"7",
"23",
"34",
"87",
"NO_TIME",
"98")

this is my code:

df %>% 
mutate(CODE=ifelse(time<1,"MENOR_1",
                    ifelse(time>=1 & time<6, "2_6_horas",
                           ifelse(time>=6 & time<12, "6_12_horas",
                                  ifelse(time=="NULL", "NO_REG",
                                         ifelse(time>=12, "MAS_DE_12","ERROR"))))))   

but the output is just not realistic


A tibble: 9 x 2
  time    CODE     
  <chr>   <chr>    
1 5       ALERT    
2 4       A        
3 NO_TIME NO_RECORD
4 7       ALERT    
5 23      A        
6 34      A        
7 87      ALERT    
8 NO_TIME NO_RECORD
9 98      ALERT 

am I doing something wrong? or is it because the data set has both numerical and categorical data? thank you so much

Aucun commentaire:

Enregistrer un commentaire