0

我正在使用 R 中的一个数据集,该数据集在我的 vector 中缺少观察结果FirstOfHCPCS.Code。我想根据另一个向量中的值对这些 NA/HCPC 代码进行编码,FirstOfService.Description. 并非每个NA都将填充相同的值,而是NA可以编码为 6 个可能的值。我尝试运行一个循环来填充 NA,但我认为因为我没有FirstOfService.Description在循环中列出 EVERY,所以 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
4

1 回答 1

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 循环和手动分配在这里非常耗时且容易出错),但有一个开始。

于 2013-07-12T18:23:47.993 回答