export-to-csv - 将数据框导出到文本文件
问题描述
有没有办法将数据框写入文本文件,并在写入时给出列的位置和长度。
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
解决方案
我不知道你到底想要什么,但你可以看看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)
推荐阅读
- javascript - 如何在 javaScript 中为 web 自定义组件上的 addEventListener 编写单元测试用例
- debugging - 调试在主题的 VS Code 中不起作用
- java - 如何为以下方法编写单元测试 JWKS RSA 算法,使用 Java 中的 junit 或 mockito 验证签名和测试角色
- trace - 采样率问题
- regex - 分隔数字和字母并重新排列字符串:可变字母的值不正确
- javascript - 如果用户缩小并且标记被吸收到 MarkerClusterer 集群中,则关闭标记上的 InfoWindow
- django - 我想限制多个用户登录,但没有以前的答案对我有帮助?
- arrays - 我想在一个空数组中添加一个三元组列表作为一整行,此方法生成一个空列表,其中包含一行和 73 列(我需要的形状)
- python - Pandas 数据框通过聚合数据删除行
- python - 为什么在我导入 getch 并且已经安装它时它会给我和 ImportError?