如果文件中有很多空白行,如何在R中使用readLines删除空白行?
我知道我可以在blank.lines.skip=T
中使用read.table
删除它,在readLines
中怎么样?
另外,如何使用readLines删除最后一个\n
?
最佳答案
一个可重现的示例:
Z <- readLines(textConnection("line1 , stuff, other stuff\nline2 ,junk\nline3, a blank two lines follow\n\n\nline6\n"))
> Z
[1] "line1 , stuff, other stuff" "line2 ,junk" "line3, a blink two lines follow"
[4] "" "" "line6"
[7] ""
> Z1 <- Z[sapply(Z, nchar) > 0] # the zero length lines get removed.
> Z1
[1] "line1 , stuff, other stuff" "line2 ,junk" "line3, a blank two lines follow"
[4] "line6"
@Andrie建议您执行以下操作:
> Z <- scan(textConnection("line1 , stuff, other stuff\nline2 ,junk\nline3, a blink two lines follow\n\n\nline6\n"),
what="", sep="\n",blank.lines.skip=TRUE)
Read 4 items
> Z
[1] "line1 , stuff, other stuff" "line2 ,junk" "line3, a blink two lines follow"
[4] "line6"
关于r - 如何在R中使用readLines删除空白行?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11865747/