2017-10-11 84 views
0

向文件写入一些文本后跟数据帧的最佳方式是什么?文本是通过将变量粘贴到字符串中创建的。r将文本和数据帧写入文件

实施例所需的输出:

Here is some text. 
This line has a variable: Hello World 
Data frame below the line 
================= 
ID,val1,val2 
1,2,3 
2,4,6 
3,6,9 
4,8,12 
5,10,15 
6,12,18 
7,14,21 
8,16,24 
9,18,27 
10,20,30 

我可以创建一个字符串与初始文本:

myvar <- "Hello World" 
out_string <- paste0("Here is some text.\n", 
        "This line has a variable: ", myvar, "\n", 
        "Data frame below the line\n", 
        "=================\n") 
cat(out_string) 

我可以写一个数据帧到文件:

library(data.table) 

mydf <- data.frame(ID = 1:10, val1 = 1:10*2, val2 = 1:10*3) 

fwrite(x = mydf, 
    file = "path/file.txt", 
    sep = ",", 
    col.names=T) 

但我我不知道如何最好地结合这两个。

我认为只是将数据帧粘贴到out_string的末尾,那么将其写入文件将是最好的,但是我的尝试失败了,例如,

cat(paste0(out_string, mydf, collapse='')) 
# Here is some text. 
# This line has a variable: Hello World 
# Data frame below the line 
# ================= 
# 1:10Here is some text. 
# This line has a variable: Hello World 
# Data frame below the line 
# ================= 
# c(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)Here is some text. 
# This line has a variable: Hello World 
# Data frame below the line 
# ================= 
# c(3, 6, 9, 12, 15, 18, 21, 24, 27, 30) 

回答

1

可能有几种方法可以做到这一点。 简单的一个

cat(out_string, file = '/tmp/test.txt') 
cat(paste0(colnames(mydf), collapse = ','), file = '/tmp/test.txt', append = T, sep = '\n') 
cat(apply(mydf,1,paste0, collapse=','), file = '/tmp/test.txt', append = T, sep = '\n') 

当然,并使用fwrite

cat(out_string, file = '/tmp/test.txt') 
fwrite(x = mydf, 
    file = "/tmp/test.txt", 
    sep = ",", 
    col.names=T, 
    append=T) 
+0

这两种方法都按要求工作。谢谢! – conor

1

一种选择是做一个连接,你可以写既writeLineswrite.csv

myvar <- "Hello World" 
out_string <- paste0("Here is some text.\n", 
        "This line has a variable: ", myvar, "\n", 
        "Data frame below the line\n", 
        "=================\n") 
mydf <- data.frame(ID = 1:10, val1 = 1:10*2, val2 = 1:10*3) 


my_file <- file('file.csv', 'w') 

writeLines(out_string, my_file, sep = '') 
write.csv(mydf, my_file, quote = FALSE, row.names = FALSE) 

close(my_file) 

readLines('file.csv') 
#> [1] "Here is some text."     
#> [2] "This line has a variable: Hello World" 
#> [3] "Data frame below the line"    
#> [4] "================="      
#> [5] "ID,val1,val2"       
#> [6] "1,2,3"         
#> [7] "2,4,6"         
#> [8] "3,6,9"         
#> [9] "4,8,12"        
#> [10] "5,10,15"        
#> [11] "6,12,18"        
#> [12] "7,14,21"        
#> [13] "8,16,24"        
#> [14] "9,18,27"        
#> [15] "10,20,30" 
+0

此方法在“out_string”和“mydf”写入之间添加一行,并输出带有引号的列名。 – conor

+0

@conor更新以删除由'writeLines'插入的分隔符('out_string'已经拥有它们)并将'quote = FALSE'参数添加到'write.csv'。我倾向于使用'readr :: write_csv'来获得更好的默认值,但将它全部保存在R中是很好的。 – alistaire

+0

感谢@alistaire,它符合现在所要求的。 – conor

1

另一种方式: sink()将打开到文件的连接。

sink("<your_new_file_name>") 
out_string 
df 
sink() 
+0

这应该有'猫(out_string)'为了正确渲染。除此之外,它几乎可以按照要求工作,但'mydf'的输出具有与空格对齐的列,而不是csv。如果你想要格式化,还是不错的。 – conor

+0

对不起,我没有注意答案格式的细节。我以为你在问如何简单地编写一个合并两个输出的文件。 – shea

+0

不用担心。我不知道“汇”,我可以看到它非常有用,所以我很感谢你的回答。 – conor