0

I am trying to create a categorical variable based of ICD codes.

head(obesity)
ICD.9.Code  Encounter.ID
E66.01    408773
E66.3     542207
E66.3     358741
E66.09    342534

obesity$obesity<- ifelse(obesity$ICD.9.Code =="E66.3", 1,      
             ifelse(obesity$ICD.9.Code=="E66.9"|obesity$ICD.9.Code=="E66.8"|obesity$ICD.9.Code=="E66.09",2,      
              ifelse(obesity$ICD.9.Code=="E66.1"|obesity$ICD.9.Code=="E66.2",3,4)))

but I keep getting this error

I want the data to look like the following, please help

ICD.9.Code  Encounter.ID  Obesity
E66.01    408773     4
E66.3     542207     1
E66.3     358741     1
E66.09    342534     2
akrun
  • 874,273
  • 37
  • 540
  • 662

2 Answers2

0

Here's a solution using data.table

obesity<-as.data.table(obesity)
obesity[ICD.9.Code == "E66.3",Obesity := 1]
obesity[ICD.9.Code == "E66.9" | ICD.9.Code == "E66.8" | ICD.9.Code == "E66.09",Obesity := 2]
obesity[ICD.9.Code == "E66.1" | ICD.9.Code == "E66.2",Obesity := 3]
obesity[is.na(Obesity),Obesity := 4]

Hope that helps.

Rage
  • 323
  • 1
  • 13
  • I figured it out, something was wrong with R, it was calling something called an is function in the methods packages. So I had to update my packages and now all the codes seem to work. I just kept having the strangest error messages. – Iman Simmonds Dec 29 '18 at 23:54
0

This might help with your current data. Can't guarantee as I've not tested on the other data.

library(tidyverse)
    mydf %>% 
      mutate(Obesity=ifelse(ICD.9.Code=="E66.3",1,c(4,2)))

EDIT: It won't work unless the IDs follow the exact pattern as in the head which is unlikely.. You might consider making multiple ifelses but that would defeat the whole purpose. A simpler way is to use a different dataframe with your encoding and bind them as suggested in the comments. Think merge,cbind. Cheers!

NelsonGon
  • 13,015
  • 7
  • 27
  • 57