有没有办法在R中写入文本文件时忽略 “\n”?

huangapple go评论90阅读模式
英文:

Is there a way to ignore "\n" while writing to a text file in R?

问题

尝试将摘要写入Excel使用文本文件。以下是R输出示例的示例。

[1] "在干预后期,响应变量的平均值约为196.08K。在没有干预的情况下,我们预期平均响应为199.41K。"

然而,当我使用write.table将其写入文本文件时,文本文件如下所示。

*在干预后期,响应变量的平均值约为196.08K。

在没有干预的情况下,我们预期平均响应为199.41K。*

以下是我用于此的代码。

write.table(analysis_text, file = "analysis_text.txt", sep = "",
            row.names = TRUE, col.names = NA)

是否有任何方法在将其写入R中的txt文件时可以忽略“/n”?

英文:

Am trying to write a summary onto excel using text file. Here is the example of how it looks like in R output.

[1] "\n\nDuring the post-intervention period, the response variable had an average value of approx.196.08K. In the absence of an intervention, we would have expected an average response of 199.41K.

However, when i write this to a text file using write.table this is how the text file looks like

*During the post-intervention period, the response variable had an average value of approx.196.08K.

In the absence of an intervention, we would have expected an average response of 199.41K.*

Here is the code am using for this.

write.table(analysis_text, file = "analysis_text.txt", sep = "",
            row.names = TRUE, col.names = NA)

Is there anyway i can ignore "/n" while writing into txt file in R?

答案1

得分: 1

如果您想忽略"\n",也许可以对您的analysis_text进行一些预处理,即gsub("\n","",analysis_text),这将删除文本中的"\n"。

在这种情况下,您的代码应该如下所示:

write.table(gsub("\n","",analysis_text), 
            file = "analysis_text.txt", 
            sep = "",
            row.names = TRUE, 
            col.names = NA)
英文:

If you want to ignore "\n", maybe you can do some pre-processing on your analisys_text, i.e., gsub("\n","",analisys_text), which removes "\n" in your texts.

In this sense, you code should be like

write.table(gsub("\n","",analisys_text), 
            file = "analysis_text.txt", 
            sep = "",
            row.names = TRUE, 
            col.names = NA)

答案2

得分: 0

查看write.table的帮助文档,该函数还可以接受一个eol参数,默认为\n

将其更改为eol="",应该就可以了。

英文:

Looking at the write.table help, the function can also take an eol argument, which defaults to \n.

Change that to eol="" and you should be fine.

huangapple
  • 本文由 发表于 2020年1月3日 17:51:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定