当我们有多个值标识同一类别时,如何在R中创建类别变量?

问题描述 投票:0回答:1

我正在尝试从分类变量中创建8个级别。我无法这样做。它可以在STATA中运行(代码如下):

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

我在R中编写了以下代码(不起作用):

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)

我收到错误消息:“不建议使用重复的因子。”

R中等效于STATA的代码将有助于解决此问题?谢谢!

r
1个回答
0
投票

我在下面重新输入了stdplaclabels,否则最终导致因素之一为“其他\ n好动”

© www.soinside.com 2019 - 2024. All rights reserved.