我有一个非常大的数据集(189 K),其中有>250个变量,其中变量是使用基于web的勾选框插入的。然后将一些变量组合成单行,例如医疗共生:
慢性肺疾病充血性心衰高血压糖尿病肥胖(BMI>35)缺血性心脏病
这个变量有1500个医疗条件的组合,如上面的线。我想突变成单独的列,例如Col 1:高血压(是/否),Col 2:糖尿病(是/否)。因此,是否存在预先存在的条件可以用作预测变量。
有办法在R中编码这个吗?
发布于 2021-12-16 11:39:51
在我看来,你拥有的数据是所有医疗条件的一个变量,由|隔开--类似于:
Patient Comorbids
1: D88310 Diabetes|Obesity (BMI>35)
2: B9939 <NA>
3: J3923 Hypertension|Obesity (BMI>35)
4: H09203 Hypertension|Diabetes|Chronic Pulmonary Disease使用来自tstrsplit()包的data.table函数将其拆分,grepl()可以将每个病人的每种疾病的存在评分如下:
# Remove braces (pay attention to these sorts of issues)
data1[, Comorbids := gsub("\\(|\\)", "", Comorbids)]
# Split the strings into individual values - unique used to find all unique values
conditions <- unique(unlist(tstrsplit(data1[, Comorbids], "\\|")))
conditions <- conditions[!is.na(conditions)]
# Score the occurence and add on to data
data2 <- data.table(data1[, -c("Comorbids")],
sapply(conditions, grepl, data1[, Comorbids]))给予:
Patient Diabetes Hypertension Obesity BMI>35 Chronic Pulmonary Disease
1: D88310 TRUE FALSE TRUE FALSE
2: B9939 FALSE FALSE FALSE FALSE
3: J3923 FALSE TRUE TRUE FALSE
4: H09203 TRUE TRUE FALSE TRUEhttps://stackoverflow.com/questions/70378091
复制相似问题