英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论