【问题标题】:Create date column in R [duplicate]在R中创建日期列[重复]
【发布时间】:2017-02-02 15:12:56
【问题描述】:

我使用 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 中,并且还没有找到根据标题匹配每日数量的方法。我愿意接受有关此问题的任何建议

【问题讨论】:

  • 尝试使用dput而不是图片向我们提供您的数据样本。
  • @user2100721 是的,我最近读过它并更新了结果,对吗?
  • 如果您有新问题,请将其作为一个问题发布。继续对原始问题进行编辑,从而使答案无效并保留答案,因为您的人质不是 hos SO 作品
  • @DavidArenburg 哦,是的。谢谢你的建议,我会改正我的错误

标签: r dataframe analysis prediction forecasting


【解决方案1】:

使用它来转换您的数据

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")

【讨论】:

    【解决方案2】:

    这是使用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,现在可以根据需要进行操作,而不是表格。

    【讨论】:

    • 感谢@Chrisss 库,我使用reshape2 包使用dcastmelt 函数。然而,还有一个障碍。现在我有 3 列 (title,quantity,date_placed),其中标题重复但 date_placedquantity 不同。如何使用重复的 title 行进行预测?
    • 如果您需要更详细的解决方案,您真的应该在您的数据上使用dput() 来帮助我们重现您的问题。到目前为止,我不明白这个问题。 dcast 正在制作一个宽 data.frame,其中 title 的唯一值作为行,date_placed 的唯一值作为列,单元格由 quantity 填充。 title 不应在最终 data.frame 中的任何位置重复
    • 是的,我最近使用 dput() 表示 R,但在预测 unique 标题值时遇到了麻烦
    【解决方案3】:

    另一个选项是spread 来自tidyr

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-19
      • 1970-01-01
      • 2022-01-15
      • 2021-01-01
      • 2018-05-23
      • 2021-11-26
      相关资源
      最近更新 更多