0

vector に観測値が欠落している R のデータセットを使用していFirstOfHCPCS.Codeます。別のベクトルの値に基づいて、これらの NAs/HCPC コードをコーディングしたいと考えていますFirstOfService.Description。すべて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に置き換えましたが、それでも上記と同じエラーが発生します。NA に複数の値を入力するにはどうすればよいですか? ご協力いただきありがとうございます!24830827186

リクエストごとに、サンプル コードと目的の出力 (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

最初に、再現可能なコードをいくつか用意しておくと便利です。これにより、何を使用しているかがわかります (データフレームが何で構成されているかはわかりません)。

それ以外の場合は、2 つの問題があるようです。

1) は使用できません== NA。代わりに、を使用してis.na()ください。

NA == NA
[1] NA
is.na(NA)
[1] TRUE

2) もう 1 つの問題は、OR ではなく AND を使用していることです。最初の例では、説明を「州の精神遅滞施設...」と「地元の精神病院...」にすることはできません。

代わりに、%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 に答える