将行附加到文件

发布于 2024-12-09 05:21:46 字数 113 浏览 1 评论 0原文

我是 R 的新手。我正在尝试使用 R 中的现有数据向文件中添加(追加)新行。问题是我的数据大约有 30000 行和 13000 列。我已经尝试使用 writeLines 函数添加一行,但生成的文件仅包含添加的行。

I'm new using R. I'm trying to add (append) new lines to a file with my existing data in R. The problem is that my data has about 30000 rows and 13000 cols. I already try to add a line with the writeLines function but the resulting file contains only the line added.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浮生未歇 2024-12-16 05:21:46

您是否尝试过使用 write函数?

line="blah text blah blah etc etc"
write(line,file="myfile.txt",append=TRUE)

Have you tried using the write function?

line="blah text blah blah etc etc"
write(line,file="myfile.txt",append=TRUE)
海螺姑娘 2024-12-16 05:21:46

write.tablewrite.csv 等都具有 append= 参数,该参数附加 append=TRUE 和如果append=FALSE,通常会覆盖。因此,您想要/必须使用哪一个取决于您的数据。

顺便说一句,cat() 也可用于将文本写入文件,并且还具有 append= 参数。

write.table, write.csv and others all have the append= argument, which appends append=TRUE and usually overwrites if append=FALSE. So which one you want to / have to use, depends on your data.

By the way, cat() can also be used to write text to a file and also has the append= argument.

伴梦长久 2024-12-16 05:21:46

您可以在附加模式下打开连接,以使用 writeLines将行附加到现有文件

writeLines("Hello", "output.txt") #Create file
CON <- file("output.txt", "a")    #Open connection to append
writeLines("World", CON)          #Append: World
writeLines("End", CON)            #Append: End
close(CON)                        #Close connection

基本相同,但使用cat,不同之处在于每次操作都会打开和关闭文件,这会降低性能。

cat("Hello\n", file = "output.txt")
cat("World\n", file = "output.txt", append = TRUE)
cat("End", file = "output.txt", append = TRUE)

cat 与连接一起使用,就像上面的 writeLines 一样。

cat("Hello\n", file = "output.txt")
CON <- file("output.txt", "a")
cat("World\n", file = CON)
cat("End", file = CON)
close(CON)

在创建文件并随后追加的情况下。

CON <- file("output.txt", "w")
writeLines("Hello", CON)
writeLines("World", CON)
writeLines("End", CON)
close(CON)

You can open a connection in append mode to append lines to an existing file with writeLines.

writeLines("Hello", "output.txt") #Create file
CON <- file("output.txt", "a")    #Open connection to append
writeLines("World", CON)          #Append: World
writeLines("End", CON)            #Append: End
close(CON)                        #Close connection

Basically the same but using cat, with the difference that the file is opened and closed on every operation, what can reduce performance.

cat("Hello\n", file = "output.txt")
cat("World\n", file = "output.txt", append = TRUE)
cat("End", file = "output.txt", append = TRUE)

Using cat with a connection, like above with writeLines.

cat("Hello\n", file = "output.txt")
CON <- file("output.txt", "a")
cat("World\n", file = CON)
cat("End", file = CON)
close(CON)

In case of creating a file and appending subsequent.

CON <- file("output.txt", "w")
writeLines("Hello", CON)
writeLines("World", CON)
writeLines("End", CON)
close(CON)
最偏执的依靠 2024-12-16 05:21:46
lapply(listOfVector, function(anyNameofVect){ write(anyNameofVect, file="outputFileName", sep="\t", append=TRUE, ncolumns=100000) })

或者

lapply(listOfVector, write, file="outputFileName", sep="\t", append=TRUE, ncolumns=100000)
lapply(listOfVector, function(anyNameofVect){ write(anyNameofVect, file="outputFileName", sep="\t", append=TRUE, ncolumns=100000) })

or

lapply(listOfVector, write, file="outputFileName", sep="\t", append=TRUE, ncolumns=100000)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文