2014-01-23 26 views
-1

我正在編寫一個函數,它將向表中寫入數據幀。每次我調用該函數時,我想在導入的數據文件的名稱後面命名該表。我熟悉使用Matlab的evalsprintf,但我正在學習R並想知道有什麼替代方案可用(我知道eval是evIl!)。使用sprintf(或其他替代方法)來命名R中的寫入表格

txtfile='datafileA.txt'; #name of data text file 
statsdf<-as.data.frame(stats) #data frame that I would like to write to a table 
txtname=str_sub(txtfile,1,-5) #portion of text file that I would like to name my table after 
#my attempt at naming the table written after the data file: 
sprintf("write.table(statsdf,'Stats_%s.txt',sep='\t',col.names=NA,quote=FALSE)",txtname) 

如何運行上面的命令?
有沒有更好的方法來做到這一點R

+0

其他替代品:'paste','substitute' – rawr

回答

2
filename <- sprintf("Stats_%s.txt", txtname) 

write.table(statsdf, file = filename, sep = "\t", col.names = NA, quote = FALSE) 
+0

非常感謝。完善。 –

+0

@NicoleGoebel可以隨意標記爲選定的答案,如果它解決了您的問題 –

+0

感謝您讓我知道這樣做。我知道SE。 –

相關問題