使用R将数据帧转换为时间序列

问题描述:

我有以下格式的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 Mist

+0

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

如果日期格式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