2017-08-31 103 views
0

我有以下格式的csv。使用R將數據幀轉換爲時間序列

Date  Sales 
1/3/2005  800 
1/4/2005 9000 
1/5/2005 1300 
1/6/2005  400 
1/7/2005  100 
1/8/2005  190 

我試圖進入它採用時間序列格式:

ts(dataframe) 

但它給人一種怪異的輸出。任何幫助或指導表示讚賞。

+0

是對第一行Dzte 1月3日或3月1日? –

+0

我想你只是想這樣'dataframe $ Date < - as.Date(dataframe $ Date,「%d /%m /%Y」)',還是我誤解? – Mist

+0

其1月3日所以是月/日/年。 – AppleGate0

回答

0

如果日期格式M/d/yyyy的(經修飾):

df <- read.table(header = TRUE, text = "Date  Sales 
1/3/2005  800 
1/4/2005 9000 
1/5/2005 1300 
1/6/2005  400 
1/7/2005  100 
1/8/2005  190", stringsAsFactors = FALSE) 

mydate <- function(x) { 
    c(year = as.numeric(unlist(strsplit(x, "/"))[3]), 
    month = as.numeric(unlist(strsplit(x, "/"))[1]), 
    day = as.numeric(unlist(strsplit(x, "/"))[2])) 
} 

(myts <- ts(df$Sales, 
      start = mydate(head(df$Date, 1)), 
      freq = 365.25)) 
+0

根據問題的討論,這是m/d/yyyy。 – neilfws