我有以下数据,我正在尝试将其设置为时间序列格式(每周)
ID Wk sales
1: 8024566 1584 73450
2: 8042245 1586 75430
3: 8044566 1589 77650
4: 8045553 1590 72345
数据的开头和结尾如下;
wk
1583 2009-12-28 - 2010-01-03
1634 2010-12-20 - 2010-12-26
增量为6天而不是7天(假设星期日休息)。我正在尝试运行将wk
列设置为日期格式以运行一些分析但似乎无法正确执行此操作。
inds <- seq(as.Date("2009-12-28"), as.Date("2010-12-26"), by = "week")
set.seed(25)
myts <- ts(yogurt_drug$WEEK(length(inds)),
start = c(2009-12-28, as.numeric(format(inds[1], "%j"))),
frequency = 52)
我收到此错误:
Error in is.data.frame(data) : attempt to apply non-function
答案 0 :(得分:1)
您的问题缺乏可复制性:我们没有yogurt_drug$WEEK
来测试您的问题。
但我认为您的问题来自yogurt_drug$WEEK(length(inds))
( )
的存在意味着使用了一个函数,但yogurt_drug$WEEK
不是一个函数,它是您的数据框。
请改为尝试:
inds <- seq(as.Date("2009-12-28"), as.Date("2010-12-26"), by = "week")
set.seed(25)
w <- lubridate::week(ymd("2009-12-28","2010-12-26"))
y <- lubridate::year(ymd("2009-12-28","2010-12-26")) # this is overkill because obviously we know that the year of "2009-12-28" is 2009, but now you know this function exists too.
myts <- ts(yogurt_drug$WEEK,
start = c(y[1],w[1]),
end = c(y[2],w[2]),
frequency = 52)
PS:如果您只想要yogurt_drug$WEEK
的项目编号长度(inds),请使用yogurt_drug$WEEK[length(inds)]
编辑:添加了使用lubridate包(不要忘记安装和加载它),以便从开始和结束日期开始一周和一年。