1

I am trying to create 8 levels from a categorical variable. I am unable to do so. It works in STATA (code as follows):

recode stdplac (11 = 2) (19 22 = 3) (20 = 4) (21 = 5) (23 = 6) (62 17 50 24 49 72 95 = 7) (31 32 61 = 8) (3 4 5 6 7 8 9 12 13 14 15 16 18 25 26 27 28 33 34 35 41 42 51 52 53 54 55 56 57 60 65 71 81 99 1 98 = 9), gen(cstdplac)
label define cstdplac_lbl 2 "Office" 3 "Outpatient" 4 "Urgent Care" 5 "Inpatient" 6 "Emergency Room" 7 "Other Ambulatory Care" 8 "Nursing Facility" 9 "Other Stdplac"
label values cstdplac cstdplac_lbl

I wrote the following code in R (it does not work):

data$stdplac <- as.integer(data$stdplac)
stdplacbreaks <- c(1,11,12,17,18,19,20,21,23,24,25,31,33,41,49,51,60,62,65,
               71,72,81,95,98,99)
stdplaclabels <- c("Other Stdplac", "Office", "Other Stdplac", "Other Ambulatory",
               "Other Stdplac", "Outpatient", "Urgent Care", "Inpatient", "Emergency Room",
               "Other Ambulatory", "Other Stdplac", "Nursing Facility", "Other Stdplac",
               "Other Stdplac", "Other Ambulatory", "Other Stdplac", "Other Stdplac", "Other 
                Ambulatory", "Other Stdplac", "Other Stdplac", "Other Ambulatory", "Other Stdplac",
               "Other Ambulatory", "Other Stdplac")
setDT(data)[ , stdplacgroups := cut(stdplac, 
                                breaks = stdplacbreaks, 
                                right = FALSE, 
                                labels = stdplaclabels)]
str(data$stdplacgroups)

I get the error saying

duplicated levels in factors are deprecated.

What would be the STATA equivalent code in R that would help solve this?

James Z
  • 12,209
  • 10
  • 24
  • 44
Jess
  • 11
  • 1
  • 1
    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. – MrFlick Nov 14 '19 at 22:28
  • 1
    But take a look at the [fct_collapse](https://forcats.tidyverse.org/reference/fct_collapse.html) function from the forcats package. – MrFlick Nov 14 '19 at 22:30
  • Package `car` has a nice `recode` function that should be able to handle this. – dcarlson Nov 14 '19 at 22:45
  • Possible duplicate https://stackoverflow.com/questions/7547597/dictionary-style-replace-multiple-items/ – Ronak Shah Nov 14 '19 at 23:57

1 Answers1

0

I re-entered stdplaclabels below, otherwise we end up one of the factors being "Other\nAmbulatory"

stdplacbreaks <- c(1,11,12,17,18,19,20,21,23,24,25,31,33,41,49,51,60,62,65,
               71,72,81,95,98,99)
stdplaclabels <- 

c("Other Stdplac", "Office", "Other Stdplac", "Other Ambulatory", 
"Other Stdplac", "Outpatient", "Urgent Care", "Inpatient", "Emergency Room", 
"Other Ambulatory", "Other Stdplac", "Nursing Facility", "Other Stdplac", 
"Other Stdplac", "Other Ambulatory", "Other Stdplac", "Other Stdplac", 
"Other Ambulatory", "Other Stdplac", "Other Stdplac", "Other Ambulatory", 
"Other Stdplac", "Other Ambulatory", "Other Stdplac")

You have a label that is not incremental, that is giving the problem. We just set ordered_result = FALSE

cut(1:100,breaks = stdplacbreaks,right = FALSE,labels = stdplaclabels,ordered_result=FALSE)

And it works:

  [1] Other Stdplac    Other Stdplac    Other Stdplac    Other Stdplac   
  [5] Other Stdplac    Other Stdplac    Other Stdplac    Other Stdplac   
  [9] Other Stdplac    Other Stdplac    Office           Other Stdplac   
 [13] Other Stdplac    Other Stdplac    Other Stdplac    Other Stdplac   
 [17] Other Ambulatory Other Stdplac    Outpatient       Urgent Care     
 [21] Inpatient        Inpatient        Emergency Room   Other Ambulatory
 [25] Other Stdplac    Other Stdplac    Other Stdplac    Other Stdplac   
 [29] Other Stdplac    Other Stdplac    Nursing Facility Nursing Facility
 [33] Other Stdplac    Other Stdplac    Other Stdplac    Other Stdplac   
 [37] Other Stdplac    Other Stdplac    Other Stdplac    Other Stdplac   
 [41] Other Stdplac    Other Stdplac    Other Stdplac    Other Stdplac   
 [45] Other Stdplac    Other Stdplac    Other Stdplac    Other Stdplac   
 [49] Other Ambulatory Other Ambulatory Other Stdplac    Other Stdplac   
 [53] Other Stdplac    Other Stdplac    Other Stdplac    Other Stdplac   
 [57] Other Stdplac    Other Stdplac    Other Stdplac    Other Stdplac   
 [61] Other Stdplac    Other Ambulatory Other Ambulatory Other Ambulatory
 [65] Other Stdplac    Other Stdplac    Other Stdplac    Other Stdplac   
 [69] Other Stdplac    Other Stdplac    Other Stdplac    Other Ambulatory
 [73] Other Ambulatory Other Ambulatory Other Ambulatory Other Ambulatory
 [77] Other Ambulatory Other Ambulatory Other Ambulatory Other Ambulatory
 [81] Other Stdplac    Other Stdplac    Other Stdplac    Other Stdplac   
 [85] Other Stdplac    Other Stdplac    Other Stdplac    Other Stdplac   
 [89] Other Stdplac    Other Stdplac    Other Stdplac    Other Stdplac   
 [93] Other Stdplac    Other Stdplac    Other Ambulatory Other Ambulatory
 [97] Other Ambulatory Other Stdplac    <NA>             <NA>   
StupidWolf
  • 45,075
  • 17
  • 40
  • 72