【发布时间】:2021-04-14 12:24:02
【问题描述】:
类似:writing a data.frame using cat 但是这是关于附加到文件,我想写一个新文件。
我有一个标头和一个数据框,我想使用 cat() 将其写入文件:
header <- "This is the top row,\n
where the values are:"
df <- data.frame(val1= runif(3), val2 = runif(3))
# Write to file
cat(header, df,
file = "path/to/file.txt",
sep = "\n")
给出错误:
Error in cat(header, df, file = "path/to/file.txt", sep = "\n") :
argument 2 (type 'list') cannot be handled by 'cat'
上面写着list,但是class(df)显示它是一个dataframe,我把这个dataframe做成和上面的基本一样。
我将如何打印它,使它看起来像这样(df 由任何类型的空格分隔):
文件.txt:
This is the top row
where the values are:
0.63138 0.70402
0.50136 0.61327
0.10447 0.26874
...
完整的文件最多可以包含 101 行的数据框。
【问题讨论】:
-
数据帧在内部实现为列表,列表的元素是列向量