在 R 中创建日期列

Create date column in R

我使用 SQL 在 RDBMS 中获取数据并希望使用 R 预测每日购买量。

这是数据的前 12 行。

我想要做的是像下图一样存储数据框,最后我会尝试编写函数来预测它使用指数平滑的行中的每个项目标题。

至此,我已经成功完成了标题栏。但是我不能像上面的第二张图片那样制作多个日期列。这是到目前为止的代码:

df1 <- data.frame() 
dailydate <- as.Date(as.POSIXct(data$date_placed))
newdate <- unique(dailydate)
itemtitle <- as.character(data$title)
newitemtitle <- unique(itemtitle)
df1 <- data.frame(newitemtitle,t(dailydate))
Error in data.frame(newitemtitle, t(dailydate))

我无法在df1中添加新栏目,而且还没有找到根据标题匹配每日数量的方法。我愿意接受有关此问题的任何建议

使用它来转换您的数据

xtabs(data = df1,quantity~title+date_placed)

数据

df1 <- structure(list(title = structure(c(5L, 3L, 6L, 1L, 7L, 2L, 1L, 
4L, 8L, 3L), .Label = c("d", "k", "m", "n", "q", "t", "u", "v"
), class = "factor"), quantity = c(4L, 3L, 5L, 10L, 6L, 13L, 
4L, 6L, 12L, 1L), date_placed = structure(c(1L, 1L, 1L, 2L, 2L, 
3L, 3L, 4L, 5L, 5L), .Label = c("8/24/2013", "8/25/2013", "8/26/2013", 
"8/27/2013", "8/28/2013"), class = "factor")), .Names = c("title", 
"quantity", "date_placed"), row.names = c(NA, -10L), class = "data.frame")

这是使用 reshape2 包的好地方。

df1 <- structure(list(title = structure(c(5L, 3L, 6L, 1L, 7L, 2L, 1L, 
4L, 8L, 3L), .Label = c("d", "k", "m", "n", "q", "t", "u", "v"
), class = "factor"), quantity = c(4L, 3L, 5L, 10L, 6L, 13L, 
4L, 6L, 12L, 1L), date_placed = structure(c(1L, 1L, 1L, 2L, 2L, 
3L, 3L, 4L, 5L, 5L), .Label = c("8/24/2013", "8/25/2013", "8/26/2013", 
"8/27/2013", "8/28/2013"), class = "factor")), .Names = c("title", 
"quantity", "date_placed"), row.names = c(NA, -10L), class = "data.frame")

#install.packages("reshape2")
reshape2:::dcast(df1, title ~ date_placed, value.var = "quantity", fill = 0)

结果:

#  title 8/24/2013 8/25/2013 8/26/2013 8/27/2013 8/28/2013
#1     d         0        10         4         0         0
#2     k         0         0        13         0         0
#3     m         3         0         0         0         1
#4     n         0         0         0         6         0
#5     q         4         0         0         0         0
#6     t         5         0         0         0         0
#7     u         0         6         0         0         0
#8     v         0         0         0         0        12

与其他答案相比,此方法的好处是输出是 data.frame,现在可以根据需要进行操作,而不是 table。

另一个选项是 spread 来自 tidyr

library(tidyr)
spread(df1, date_placed, quantity, fill = 0)