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?