0

I have a column with some numbers, each corresponding to a primary condition of a patient. I want to recode this data into types of conditions ex. neurological, psychiatric etc

Can you please help as the code below is horrendous (and also not working)

October_data_UK$Primary_cat <- ifelse(October_data_UK$PRIMARY==2|October_data_UK$PRIMARY==5|October_data_UK$PRIMARY==7|October_data_UK$PRIMARY==10|October_data_UK$PRIMARY==13|October_data_UK$PRIMARY==14|October_data_UK$PRIMARY==16|October_data_UK$PRIMARY==23|October_data_UK$PRIMARY==24|October_data_UK$PRIMARY==27,"Chronic_pain",
                               ifelse(October_data_UK$PRIMARY==4|October_data_UK$PRIMARY==9|October_data_UK$PRIMARY==15|October_data_UK$PRIMARY==21|October_data_UK$PRIMARY==22|October_data_UK$PRIMARY==31|October_data_UK$PRIMARY==35|October_data_UK$PRIMARY==37|October_data_UK$PRIMARY==38, "Neurological",
                               ifelse(October_data_UK$PRIMARY==1|October_data_UK$PRIMARY==3|October_data_UK$PRIMARY==6|October_data_UK$PRIMARY==12|October_data_UK$PRIMARY==17|October_data_UK$PRIMARY==18|October_data_UK$PRIMARY==20|October_data_UK$PRIMARY==25|October_data_UK$PRIMARY==26,October_data_UK$PRIMARY==30|October_data_UK$PRIMARY==32|October_data_UK$PRIMARY==34|October_data_UK$PRIMARY==36,"Psychiatric",
                               ifelse(October_data_UK$PRIMARY==8|October_data_UK$PRIMARY==11|October_data_UK$PRIMARY==19|October_data_UK$PRIMARY==33|October_data_UK$PRIMARY==28|October_data_UK$PRIMARY==29|October_data_UK$PRIMARY==39,"Other",NA))))
                                             

I just want to write all the numbers together without having to repeat "October_data_UK$PRIMARY"

NoobR
  • 57
  • 5
  • All of those `|` (or) statements can be replaced with an `%in%`. For example `October_data_UK$PRIMARY %in% c(2, 5, 7, 10, 13, 14, 16, 23, 24, 27)`. Or even better create a look up data.frame with a column for PRIMARY and category and then join the data together. – MrFlick Oct 28 '22 at 15:30
  • 1
    If you have a table of number vs condition, then an easier way is to `merge()` or `dplyr::left_join()` the two dataframes together. – Dave2e Oct 28 '22 at 15:42
  • @MrFlick I tried this and it doesn't work it comes up with " 'list' object cannot be coerced to type 'logical'" – NoobR Dec 02 '22 at 14:25
  • What exactly did you try? Perhaps there was some other error that was introduced or maybe your data isn't in the format one might expect. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. If we can't run the code, it's hard to know what's going on – MrFlick Dec 02 '22 at 14:30

1 Answers1

0

Try assigning the values with logical indices.

Chronic_pain <- c(2, 5, 7, 10, 13, 14, 16, 23, 24, 27)
Neurological <- c(4, 9, 15, 21, 22, 31, 35, 37, 38)
Psychiatric <- c(1, 3, 6, 12, 17, 18, 20, 25, 26, 30, 32, 34, 36)
Other <- c(8, 11, 19, 33, 28, 29, 39)

i1 <- October_data_UK$Primary_cat %in% Chronic_pain
i2 <- October_data_UK$Primary_cat %in% Neurological
i3 <- October_data_UK$Primary_cat %in% Psychiatric
#i4 <- October_data_UK$Primary_cat %in% Other

October_data_UK$Primary_cat <- NA_character_      # this creates the column
October_data_UK$Primary_cat[i1] <- "Chronic_pain"
October_data_UK$Primary_cat[i2] <- "Neurological"
October_data_UK$Primary_cat[i3] <- "Psychiatric"
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66