fork download
  1. Sys.setlocale(category = "LC_ALL", locale = "cht") # for windows user
  2. library(magrittr)
  3. library(data.table)
  4. library(zoo)
  5.  
  6. con <- file("date.txt", "r", blocking = FALSE)
  7. d <-
  8. readLines(con, encoding = "UTF-8") %>% # 讀入文字檔
  9. trimws %>% # 除去前後空白
  10. gsub(" +", " ", .) %>% # 除去多餘空白
  11. as.data.table %>%
  12. setnames(".", "rawTxt") %>%
  13. .[-1:-3] # 刪除前三列
  14. close(con)
  15.  
  16. rowIDs1 <- grep("^標 準 別:", d$rawTxt) # 找到「標準別」列
  17. rowIDs2 <- which(!(1:nrow(d)) %in% rowIDs1) # 找到非「標準別」列
  18.  
  19. # 捉取並寫入「標準別」列的「標準別」欄
  20. d[rowIDs1, 標準別 := gsub("標 準 別:", "", rawTxt)]
  21. # 由上一個非NA值填入標準別
  22. d[, 標準別 := na.locf(標準別)]
  23.  
  24. # 捉取並寫入非「標準別」列各欄
  25. d[rowIDs2, 處置代碼 := tstrsplit(rawTxt, " ", keep = 1)]
  26. d[rowIDs2, 處置名稱 := tstrsplit(rawTxt, " ", keep = 2)]
  27. d[rowIDs2, 處置類別 := tstrsplit(rawTxt, " ", keep = 3)]
  28. d[rowIDs2, 健保單價 := tstrsplit(rawTxt, " ", keep = 4)]
  29. d[rowIDs2, 自費單價 := tstrsplit(rawTxt, " ", keep = 5)]
  30.  
  31. # 刪除「標準別」列
  32. d <- d[rowIDs2]
  33. # 刪除rawTxt
  34. d[, rawTxt := NULL]
  35. # 完工
  36. print(d)
  37. # 標準別 處置代碼 處置名稱 處置類別 健保單價 自費單價
  38. # 1: 030 診察費 10101A 門診診察費 F11 80.00 80.00
  39. # 2: 030 診察費 TR-IN 急診轉入醫院獎勵 F 500.00 500.00
  40. # 3: 030 診察費 TR-OUT 急診轉出醫院獎勵 F 500.00 500.00
  41. # 4: 030 診察費 10201B 急診診察費-檢傷分類第一級 F14 1800.00 1800.00
  42. # 5: 030 診察費 10202B 急診診察費-檢傷分類第二級 F14 1000.00 1000.00
  43. # ---
  44. # 117: 240 一般檢查費 E3031C BI:SGPT(替代療法) D01 50.00 50.00
  45. # 118: 240 一般檢查費 E3032C BI:r-GT(替代療法) D01 70.00 70.00
  46. # 119: 240 一般檢查費 E3034C 愛滋病毒篩檢暨衛教諮詢費 D01 225.00 225.00
  47. # 120: 240 一般檢查費 E3034C-1 愛滋病毒篩檢費-自付差額(複診) D01 15.00 15.00
  48. # 121: 240 一般檢查費 E3037C C型肝炎病毒抗體檢驗暨諮詢費 D01 350.00 350.00
  49.  
  50. # 轉存CSV檔
  51. d %>% as.data.frame %>% write.csv(., "dateNew2.csv", fileEncoding = "UTF-8")
  52.  
Success #stdin #stdout #stderr 0.41s 42948KB
stdin
Standard input is empty
stdout
[1] ""
stderr
Warning message:
In Sys.setlocale(category = "LC_ALL", locale = "cht") :
  OS reports request to set locale to "cht" cannot be honored

Attaching package: ‘zoo’

The following objects are masked from ‘package:base’:

    as.Date, as.Date.numeric

Error in file("date.txt", "r", blocking = FALSE) : 
  cannot open the connection
In addition: Warning message:
In file("date.txt", "r", blocking = FALSE) :
  cannot open file 'date.txt': No such file or directory
Execution halted