2017-03-21 40 views
2

我想輸出我的數據框在R到Excel文件與write.xlsx2函數。R:write.xlsx2文件名作爲Sys.time()

雖然我想將文件名設置爲今天的數據。

我試過代碼如下:

paste(c(format(Sys.time(), "%m%d%Y"), '.xlsx'), sep="") 

,結果是

[1] "03202017" ".xlsx" 

好。我得擺脫那個引號。

noquote(paste0(c(format(Sys.time(), "%m%d%Y"),'.xlsx'), sep ="")) 

看來工作,但結果已在如下之間惱人的空間:

write.xlsx2(fcst,noquote(paste0(c(format(Sys.time(), "%m%d%Y"),'.xlsx'), sep ="")) , sheetName = "Sheet1", append=F) 

[1] 03202017 .xlsx 

最糟糕的是它不write.xlsx2功能工作

錯誤消息如下:

Error in createWorkbook(type = ext) : 
    Unknown format 03202017Unknown format xlsx 
In addition: Warning messages: 
1: In if (type == "xls") { : 
    the condition has length > 1 and only the first element will be used 
2: In if (type == "xlsx") { : 
    the condition has length > 1 and only the first element will be used 

我做了什麼錯了?我怎樣才能刪除之間的空間。

如何在文件名中輸出當前日期的excel文件?

謝謝。

+1

你試過了嗎:paste0(格式(Sys.time(),「%m%d%Y」),'。xlsx') – Dave2e

+0

它工作了......不知道爲什麼我的大腦總是錯過最簡單的東西。謝謝你。 – wjang4

回答

0

下面是一些代碼,我已經使用了這種事情之前,使用stringr包:

require(stringr) 
todaysdate <- paste(str_match(Sys.Date(), "..(..)-(..)-(..)")[,c(3,4,2)], collapse=".") 

當然,你可以改變collapse參數匹配要與分隔日期的數字是什麼,包括""不分離。