如何在split()之后对数据进行子集化

时间:2017-08-08 18:02:26

标签: r database dataframe data-manipulation

只是想知道是否有人知道在分割后如何使用数据。这就是我现在所拥有的。

combined_cost_freq_2_inp <- run_sql("m5c_comb_out_name.sql", cond_str)

    checker <- subset(combined_cost_freq_2_inp, is.na(combined_cost_freq_2_inp$inp_allowed))

    holders <- split(combined_cost_freq_2_inp, list(combined_cost_freq_1$cd1,combined_cost_freq_1$cd2))

 if( is.na(checker$inp_allowed) == TRUE )
    {

    sub1 <- subset(holders, !is.na(inp_allowed) & svc_code_category == "Facility - Inpatient")
    sub2 <- subset(holders, is.na(inp_allowed)& svc_code_category == "Facility - Inpatient")

    sum_freq_0 <- sum(sub2$svcc_pos_freq)

    sum_freq_div <- sum_freq_0 / length(sub1$svcc_pos_freq)

    sum_freq_added <- (sub1$svcc_pos_freq + sum_freq_div)

    if( sum_freq_added > 1)
        {
            sub1$svcc_pos_freq <- 1
        }
    else    
        {
            sub1$svcc_pos_freq <- sum_freq_added
        }


holder <- rbind(sub1, sub2) 

combined_cost_freq_2_inp <- holder

拆分下方的代码在拆分之前工作得很好,但现在我意识到我需要拆分独特的值,这确实让事情变得比我想要的复杂得多,所以任何帮助都会非常感激!

示例数据: 注意:dput(head(holder,5))只是为了发布

Browse[2]> str(holders)
List of 1
 $ Surgical Treatment.Laparoscopic Gallbladder Removal (Cholecystectomy):'data.frame':  1392 obs. of  26 variables:
  ..$ state            : chr [1:1392] "MO" "MO" "MO" "MO" ...
  ..$ hrrcity          : chr [1:1392] "Cape Girardeau" "Cape Girardeau" "Cape Girardeau" "Cape Girardeau" ...
  ..$ mcp_category     : chr [1:1392] "Digestive Conditions" "Digestive Conditions" "Digestive Conditions" "Digestive Conditions" ...
  ..$ diagnosis_group  : chr [1:1392] "Gallstones" "Gallstones" "Gallstones" "Gallstones" ...
  ..$ cd1              : chr [1:1392] "Surgical Treatment" "Surgical Treatment" "Surgical Treatment" "Surgical Treatment" ...
  ..$ cd2              : chr [1:1392] "Laparoscopic Gallbladder Removal (Cholecystectomy)" "Laparoscopic Gallbladder Removal (Cholecystectomy)" "Laparoscopic Gallbladder Removal (Cholecystectomy)" "Laparoscopic Gallbladder Removal (Cholecystectomy)" ...
  ..$ cd3              : chr [1:1392] "Inpatient Hospital" "Inpatient Hospital" "Inpatient Hospital" "Inpatient Hospital" ...
  ..$ timeline_ind     : chr [1:1392] "Evaluation" "Evaluation" "Evaluation" "Evaluation" ...
  ..$ svc_lvl_code     : chr [1:1392] "" "Consultation and Management" "Consultation and Management" "Consultation and Management" ...
  ..$ svc_code_category: chr [1:1392] "74174" "Initial hospital care, per day (70 minutes)" "Initial observation care visit, high complexity" "Office visit, 40 minutes" ...
  ..$ svcc_pos         : chr [1:1392] "" "" "" "" ...
  ..$ claim_type       : chr [1:1392] "" "" "" "" ...
  ..$ ep_count         : int [1:1392] 14 14 14 14 14 14 14 14 14 14 ...
  ..$ svcc_freq        : num [1:1392] 0.0714 0.0714 0.0714 0.2857 0.0714 ...
  ..$ svcc_pos_freq    : num [1:1392] 0.0714 0.0714 0.0714 0.2857 0 ...
  ..$ avg_services     : num [1:1392] 1 1 1 2 1 1 1 1 1 1 ...
  ..$ pos_indicator    : chr [1:1392] NA "" "" "" ...
  ..$ average_billed   : num [1:1392] NA 389 440 266 651 ...
  ..$ average_allowed  : num [1:1392] NA 215.8 196.2 151.7 51.6 ...
  ..$ rep_code         : chr [1:1392] NA NA NA NA ...
  ..$ rx_brand_name    : chr [1:1392] NA NA NA NA ...
  ..$ rx_generic_name  : chr [1:1392] NA NA NA NA ...
  ..$ rx_avg_cost      : num [1:1392] NA NA NA NA NA NA NA NA NA NA ...
  ..$ drg_id           : chr [1:1392] NA NA NA NA ...
  ..$ inp_billed       : num [1:1392] NA NA NA NA NA NA NA NA NA NA ...
  ..$ inp_allowed      : num [1:1392] NA NA NA NA NA NA NA NA NA NA ...

0 个答案:

没有答案