簡體   English   中英

使用R將數據幀轉換為時間序列

[英]Converting Data Frame to Time Series using R

我有以下格式的csv。

  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)

但是它給出了一個奇怪的輸出。 任何幫助或指導表示贊賞。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM