首页 > 解决方案 > 将数据框导出到文本文件

问题描述

有没有办法将数据框写入文本文件,并在写入时给出列的位置和长度。

df <- data.frame(
    col1 <- c(1, 2, 3),
    col2 <- c("name1", "name2", "name3"),
    col3 <- c("place1", "place2", "place3")
)

我想将 df 导出到 col1 长度 = 1 且位置 = 1 的文本文件中;col2 的长度 = 20,位置 = 2,col3 的长度 = 60,位置 = 21

标签: export-to-csv

解决方案


我不知道你到底想要什么,但你可以看看write.table()

write.table(df, file="df.txt")

我希望这有帮助。

编辑

您还可以看看如何在 R 中写入 .txt 文件?让 R 用回车写入 .txt。在那里,他们建议人们可以而且实际上在某些情况下应该考虑fwrite.

编辑 2 如果您的输出 txt 看起来像

1--name1--place1
2--name2--place2
3--name3--place3

你需要

write.table(
    df, file="df.txt", row.names=FALSE, col.names=FALSE, sep="--", quote=FALSE)

推荐阅读