我正在編寫一個函數,它將向表中寫入數據幀。每次我調用該函數時,我想在導入的數據文件的名稱後面命名該表。我熟悉使用Matlab的eval
和sprintf
,但我正在學習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?
其他替代品:'paste','substitute' – rawr