如何将数据帧写入文件,以便无论列值的长度如何,值都是相邻的,没有空格

How to write dataframe to file so that regardless of length of column values, the values are adjacent without spaces

我有一个如下所示的数据框:

V1 V2 V3
1 06  1
06 1  1
0 08  1
08 0  1
0 06  1
06 0  1

我想以固定宽度格式写入文本文件,使其看起来像这样:

1061
0611
0081
0801
0061
0601

目前,我正在使用来自 gdata 的 write.fwf:

write.fwf(colnames=FALSE, x=df,file='file.txt', sep="")

但这会产生如下输出:

1 061
061 1
0 081
080 1
0 061
060 1

.. 空格被保留。

我想我使用 rowwise() mutate() 将每一行转换为单个值,然后输出,或者使用 read.table 转换为单行——尽管这似乎会占用前导零,我需要

但我想看看完成这个任务最直观的方法是什么。

您可以按照评论中的建议将其转换为 R 中的向量,或使用:

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