gpt4 book ai didi

删除 R 中空格前的换行符

转载 作者:行者123 更新时间:2023-12-04 15:29:50 25 4
gpt4 key购买 nike

如果下一行在 R 中以空格或制表符开头,我想删除换行符。到目前为止,我已经尝试使用正则表达式:

text <- gsub("\n(?=\\h)","",x,perl=True)

但是没有用

最佳答案

你可以使用

text <- gsub("\\R(?=\\h)", "", x, perl=TRUE)

\R(?=\h) pattern 匹配任何水平空格之前的任何换行符序列(与 \R 匹配)(使用 (?=\h) 正前瞻检查是否存在)。

查看 R demo :

x <- "Line 1\r\n coninuation of line 1\r\nLine 2"
gsub("\\R(?=\\h)", "", x, perl=TRUE)
## => [1] "Line 1 coninuation of line 1\r\nLine 2"

关于删除 R 中空格前的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61447958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com