Home > Mobile >  How to create a variable based on character and number iteration in R?
How to create a variable based on character and number iteration in R?

Time:06-17

I'm trying to create a dummy variable based on the character type variable.

For example, I need to create "newcat" variable ranging from "I00" to "I99". In the code I wrote, I place all the characters from I00-I99. But is there any way to make this code efficient with the loop to iterate number after the string? Thank you in advance!!

mort <- mort %>% 
   mutate(newcat = ifelse(ucod=="I00" | 
                         ucod=="I01" | ucod=="I02" | ucod=="I03" | ucod=="I04" | ucod=="I05" | 
                         ucod=="I06" | ucod=="I07" | ucod=="I08" | ucod=="I09" | ucod=="I10" |  
                         ucod=="I11" | ucod=="I12" | ucod=="I13" | ucod=="I14" | ucod=="I15" | 
                         ucod=="I16" | ucod=="I17" | ucod=="I18" | ucod=="I19" | ucod=="I20" | 
                         ucod=="I21" | ucod=="I22" | ucod=="I23" | ucod=="I24" | ucod=="I25" | 
                         ucod=="I26" | ucod=="I27" | ucod=="I28" | ucod=="I29" | ucod=="I30" |  
                         ucod=="I31" | ucod=="I32" | ucod=="I33" | ucod=="I34" | ucod=="I35" | 
                         ucod=="I36" | ucod=="I37" | ucod=="I38" | ucod=="I39" | ucod=="I40" | 
                         ucod=="I41" | ucod=="I42" | ucod=="I43" | ucod=="I44" | ucod=="I45" | 
                         ucod=="I46" | ucod=="I47" | ucod=="I48" | ucod=="I49" | ucod=="I50" |  
                         ucod=="I51" | ucod=="I52" | ucod=="I53" | ucod=="I54" | ucod=="I55" | 
                         ucod=="I56" | ucod=="I57" | ucod=="I58" | ucod=="I59" | ucod=="I60" | 
                         ucod=="I61" | ucod=="I62" | ucod=="I63" | ucod=="I64" | ucod=="I65" | 
                         ucod=="I66" | ucod=="I67" | ucod=="I68" | ucod=="I69" | ucod=="I70" |  
                         ucod=="I71" | ucod=="I72" | ucod=="I73" | ucod=="I74" | ucod=="I75" | 
                         ucod=="I76" | ucod=="I77" | ucod=="I78" | ucod=="I79" | ucod=="I80" | 
                         ucod=="I81" | ucod=="I82" | ucod=="I83" | ucod=="I84" | ucod=="I85" | 
                         ucod=="I86" | ucod=="I87" | ucod=="I88" | ucod=="I89" | ucod=="I90" | 
                         ucod=="I91" | ucod=="I92" | ucod=="I93" | ucod=="I94" | ucod=="I95" | 
                         ucod=="I96" | ucod=="I97" | ucod=="I98" | ucod=="I99", 1, 0))

CodePudding user response:

Try %in% instead of == with |

x <- c(paste0("I0", 0:9),paste0("I", c(10:99)))
mort %>% 
  mutate(newcat = ifelse(ucod %in% x, 1, 0))

CodePudding user response:

Another option is to use regex:

mort <- mort %>% 
   mutate(newcat =  str_detect(ucod, '^I[0-9]{2}$'))

where ^ is a metacharacter which indicates the beginning of the string. Then we have I[0-9]{2} which matches the letter I and any 2 combinations of the numbers 0-9. Then $ is another metacharacter that indicates the end of the string. So the string matched must start with I followed by 2 numbers and that should be the end of the string. Any string that does not match the pattern will be flaged as FALSE

  • Related