【发布时间】:2018-07-09 09:56:59
【问题描述】:
我一直在使用 tidyr 将一些文本分成几列。
输入
Mypathcolon <- data.frame(c("1 Hospital: Random NHS Foundation Trust\nHospital Number: H2890235\nPatient Name: al-Bilal, Widdad\nDOB: 1922-05-04\nGeneral Practitioner: Dr. Mondragon, Amber\nDate received: 2002-11-10\nClinical Details: Previous had serrated lesions ?,If looks more like UC, please provide Nancy severity index\n3 specimen. Nature of specimen: Nature of specimen as stated on pot = 'Ascending colon x2 '|,Nature of specimen as stated on request form = 'rectum'|,Nature of specimen as stated on pot = '4X LOWER, 4X UPPER OESOPHAGUS '|,Nature of specimen as stated on pot = 'rectal polyp '|\nMacroscopic description: 1 specimens collected the largest measuring 3 x 5 x 2 mm and the smallest 3 x 5 x 5 mm\nHistology: The appearances are of a hyperplastic polyp.,8 pieces of tissue, the largest measuring 4."))
names(Mypathcolon)<-c("PathReportWhole")
函数
Histoltree <- c("Hospital Number:","Patient Name:",
"DOB:","General Practitioner:","Date received:",
"Clinical Details","Nature of specimen",
"Macroscopic description:","Histology","Diagnosis")
Mypathcolon %>%
tidyr::separate(PathReportWhole,
into = c("added_name",Histoltree),
sep = paste(Histoltree, collapse = "|"))
当前输出
这给了我列名
[1] "added_name" "Hospital Number:" "Patient Name:" "DOB:"
[5] "General Practitioner:" "Date received:" "Clinical Details" "Nature of specimen"
[9] "Macroscopic description:" "Histology" "Diagnosis"
但是“标本性质”到“诊断”栏目中的数据实际上在正文中包含了“标本性质”到“标本性质”的文字,而不是“标本性质”到“宏观描述”的文字应该是这样:实际输出见下文:
structure(list(added_name = "1 Hospital: Random NHS Foundation Trust\n",
`Hospital Number:` = " H2890235\n", `Patient Name:` = " al-Bilal, Widdad\n",
`DOB:` = " 1922-05-04\n", `General Practitioner:` = " Dr. Mondragon, Amber\n",
`Date received:` = " 2002-11-10\n", `Clinical Details` = ": Previous had serrated lesions ?,If looks more like UC, please provide Nancy severity index\n3 specimen. ",
`Nature of specimen` = ": ", `Macroscopic description:` = " as stated on pot = 'Ascending colon x2 '|,",
Histology = " as stated on request form = 'rectum'|,", Diagnosis = " as stated on pot = '4X LOWER, 4X UPPER OESOPHAGUS '|,"), .Names = c("added_name",
"Hospital Number:", "Patient Name:", "DOB:", "General Practitioner:",
"Date received:", "Clinical Details", "Nature of specimen", "Macroscopic description:",
"Histology", "Diagnosis"), row.names = 1L, class = "data.frame")
如何强制函数提取列出的分隔符之间的列,而不是它似乎正在做的重复提取。
想要的输出
Hospital: Random NHS Foundation Trust\n
Hospital Number: H2890235\n
Patient Name: al-Bilal, Widdad\n
DOB: 1922-05-04\n
General Practitioner: Dr. Mondragon, Amber\n
Date received: 2002-11-10\n
Clinical Details: Previous had serrated lesions ?,If looks more like UC, please provide Nancy severity index\n3 specimen.
Nature of specimen: Nature of specimen as stated on pot = 'Ascending colon x2 '|,Nature of specimen as stated on request form = 'rectum'|,Nature of specimen as stated on pot = '4X LOWER, 4X UPPER OESOPHAGUS '|,Nature of specimen as stated on pot = 'rectal polyp '|\n
Macroscopic description: 1 specimens collected the largest measuring 3 x 5 x 2 mm and the smallest 3 x 5 x 5 mm\n
Histology: The appearances are of a hyperplastic polyp.,8 pieces of tissue, the largest measuring 4.
【问题讨论】: