我正在处理R中的数据集,该数据集在我的向量FirstOfHCPCS.Code
中缺少观察结果。我想根据另一个向量FirstOfService.Description
中的值对这些NAs / HCPC代码进行编码。不是每个NA
都会填充相同的值,而是NA
可以编码为6个可能的值。我尝试运行循环来填充NA,但我认为因为循环中没有列出FirstOfService.Description
,所以R不知道如何处理这些值。这是我的循环代码和结果错误(用金丝雀的建议更新):
for (i in 1:248308){
if (is.na(Master$FirstOfHCPCS.Code[i])&Master$FirstOfService.Description[i]%in%c("State Mental Retardation Facility - Inpatient (ICF/MR) PT65",
"Local Psychiatric Hospital/IMD PT68", "Local Psychiatric Hospital - Acute Community PT73","State Psychiatric Hospital - Inpatient PT22"))
{Master$FirstOfHCPCS.Code[i]=2}
if (is.na(Master$FirstOfHCPCS.Code[i])&Master$FirstOfService.Description[i]%in%c("Inpatient Hospital Ancillary Services - Room and Board",
"Inpatient Hospital Ancillary Services - Leave of Absence",
"Inpatient Hospital Ancillary Services - Pharmacy",
"Inpatient Hospital Ancillary Services - Medical/Surgical Supplies and Devices",
"Inpatient Hospital Ancillary Services - Laboratory",
"Inpatient Hospital Ancillary Services -EKG/ECG",
"Inpatient Hospital Ancillary Services - EEG",
"Inpatient Hospital Ancillary Services - Psychiatric/Psychological Treatments/Services",
"Inpatient Hospital Ancillary Services - Other Diagnosis Services",
"Inpatient Hospital Ancillary Services - Other Therapeutic Services"=="Inpatient Hospital Ancillary Services - Radiology",
"Inpatient Hospital Ancillary Services - Respiratory Services",
"Inpatient Hospital Ancillary Services -Physical Therapy",
"Inpatient Hospital Ancillary Services - Occupational Therapy",
"Inpatient Hospital Ancillary Services - Speech-Language Pathology",
"Inpatient Hospital Ancillary Services - Emergency Room",
"Inpatient Hospital Ancillary Services - Pulmonary Function",
"Inpatient Hospital Ancillary Services - Audiology",
"Inpatient Hospital Ancillary Services - Magnetic Resonance Technology (MRT)",
"Inpatient Hospital Ancillary Services - Pharmacy",
"Additional Codes-ECT Facility Charge")){Master$FirstOfHCPCS.Code[i]=1}
if (is.na(Master$FirstOfHCPCS.Code[i])&Master$FirstOfService.Description[i]%in%c("Pharmacy (Drugs and Other Biologicals)")){Master$FirstOfHCPCS.Code[i]=3}
if (is.na(Master$FirstOfHCPCS.Code[i])&Master$FirstOfService.Description[i]%in%c("Crisis Observation Care")){Master$FirstOfHCPCS.Code[i]=4}
if (is.na(Master$FirstOfHCPCS.Code[i])&Master$FirstOfService.Description[i]%in%c("Outpatient Partial Hospitalization")){Master$FirstOfHCPCS.Code[i]=5}
if (is.na(Master$FirstOfHCPCS.Code[i])&Master$FirstOfService.Description[i]%in%c("Other")){Master$FirstOfHCPCS.Code[i]=6}}
Error in if (is.na(Master$FirstOfHCPCS.Code[i]) & Master$FirstOfService.Description[i] %in% :
argument is of length zero
我还运行了sum(is.na(Master$FirstOfHCPCS.Code))
以找出我使用NA
的行数,然后用该数字(248308
)替换循环代码中的27186
但是我仍然会得到与上面相同的错误。如何用多个值填充NA?谢谢你的帮助!
按请求,示例代码和所需输出(Desired_FirstOfHCPCS.Code)
##Sample Code##
FirstOfService.Description<-c("State Mental Retardation Facility - Inpatient (ICF/MR) PT65","Wraparound", "Inpatient Hospital Ancillary Services - Room and Board",
"Pharmacy (Drugs and Other Biologicals)","Local Psychiatric Hospital - Acute Community PT73","State Psychiatric Hospital - Inpatient PT22","Case Management","Crisis Observation Care","Outpatient Partial Hospitalization",
"Other")
Desired_FirstOfHCPCS.Code<-c(2, 85, 1, 3, 2, 2, 11, 4, 5, 6)
FirstOfHCPCS.Code<-c(NA, 85, NA, NA, NA, NA, 11, NA, NA, NA)
df<-data.frame(FirstOfService.Description, FirstOfHCPCS.Code)
df
输出:
FirstOfService.Description FirstOfHCPCS.Code
1 State Mental Retardation Facility - Inpatient (ICF/MR) PT65 NA
2 Wraparound 85
3 Inpatient Hospital Ancillary Services - Room and Board NA
4 Pharmacy (Drugs and Other Biologicals) NA
5 Local Psychiatric Hospital - Acute Community PT73 NA
6 State Psychiatric Hospital - Inpatient PT22 NA
7 Case Management 11
8 Crisis Observation Care NA
9 Outpatient Partial Hospitalization NA
10 Other NA
我希望它看起来像:
#Desired Output
df2<-data.frame(FirstOfService.Description, Desired_FirstOfHCPCS.Code)
df2
FirstOfService.Description Desired_FirstOfHCPCS.Code
1 State Mental Retardation Facility - Inpatient (ICF/MR) PT65 2
2 Wraparound 85
3 Inpatient Hospital Ancillary Services - Room and Board 1
4 Pharmacy (Drugs and Other Biologicals) 3
5 Local Psychiatric Hospital - Acute Community PT73 2
6 State Psychiatric Hospital - Inpatient PT22 2
7 Case Management 11
8 Crisis Observation Care 4
9 Outpatient Partial Hospitalization 5
10 Other 6
答案 0 :(得分:2)
首先,拥有一些可重现的代码是有用的,这样我们就知道你正在使用什么(我们不知道你的数据帧是什么)。
否则,看起来有两个问题。
1)你不能使用== NA
;相反,使用is.na()
。
NA == NA
[1] NA
is.na(NA)
[1] TRUE
2)另一个问题是你使用的是AND而不是OR。在第一个例子中,你的描述不能是“国家精神发育迟滞设施......”和“地方精神病医院......”。
相反,请尝试使用%in%
例如
is.na(Master$FirstOfHCPCS.Code[i]) &
Master$FirstOfService.Description[i] %in% c("State Mental Retardation Facility - Inpatient (ICF/MR) PT65", "Local Psychiatric Hospital/IMD PT68")
还有很多其他方法可以清除这些代码(for循环和手动分配非常耗时且容易出错),但是有一个开始。