这是一个测试用例。您只想删除空行。这是文件test.txt(包含拼写错误):
(注意:您的示例显然不是 csv 文件。)
some header text
more text
even omre text
------
txt= readLines("test.txt")
newtext <- txt[nchar(txt)>0]
newtext
#[1] "some header text" "more text" " even omre text"
要删除编号的行(以数字开头,后跟句点的行),可以使用 sub() 发布结果:
txt <- "PAST MEDICAL HISTORY
1. Persistent atrial fibrillation with atrial flutter, status-post atrial flutter ablation line in October of 2002.
2. Tachy/brady syndrome.
3. Insulin-dependent diabetes. Has been diabetic for approximately 35 years.
4. Hypertension, well"
newtxt= readLines(textConnection(txt))
sub("^[[:digit:].]+", "", newtxt)
#------------------------
[1] "PAST MEDICAL HISTORY"
[2] ""
[3] " Persistent atrial fibrillation with atrial flutter, status-post atrial flutter ablation line in October of 2002."
[4] " Tachy/brady syndrome."
[5] " Insulin-dependent diabetes. Has been diabetic for approximately 35 years. "
[6] " Hypertension, well"
> sub("^[[:digit:].]+", "", newtxt[nchar(newtxt)>0])
[1] "PAST MEDICAL HISTORY"
[2] " Persistent atrial fibrillation with atrial flutter, status-post atrial flutter ablation line in October of 2002."
[3] " Tachy/brady syndrome."
[4] " Insulin-dependent diabetes. Has been diabetic for approximately 35 years. "
[5] " Hypertension, well"