【问题标题】:Mutate a lead or lag column for certain rows in r为 r 中的某些行改变领先或滞后列
【发布时间】:2016-10-11 02:10:40
【问题描述】:

我的预测与移动假期不太一致。我正在尝试快速解决:

这是我的数据框的结构:

df1:
Date         City         Visitors      WKN    WKN_2015   Holiday
2016-11-06   New York     40000         45     46         No_Holiday
2016-11-13   New York     50000         46     47         No_Holiday
2016-11-20   New York     50000         47     48         Thanksgiving
2016-11-27   New York     100000        48     49         Cyber_Monday
2016-12-04   New York     100000        49     50         No_Holiday
2016-12-11   New York     70000         50     51         No_Holiday
.
.
.
2017-11-23   New York     120000        47     47         Thanksgiving

一般来说,感恩节和网络星期一会有更多的游客来这座城市。但我的预测并没有反映出这一点。现在我想要一个快速修复这样的事情:

df1:
Date         City         Visitors      WKN    WKN_2015   Holiday        New_Visitors
2016-11-06   New York     40000         45     46         No_Holiday     40000 
2016-11-13   New York     50000         46     47         No_Holiday     50000     
2016-11-20   New York     50000         47     48         Thanksgiving   100000
2016-11-27   New York     100000        48     49         Cyber_Monday   100000
2016-12-04   New York     100000        49     50         No_Holiday     70000
2016-12-11   New York     70000         50     51         No_Holiday     70000
.
.
.
2017-11-23   New York     120000        47     47         Thanksgiving   120000

如果您看到上述数据 新成交量仅在感恩节、网络星期一和网络星期一后一周发生变化。 有什么方法可以自动执行此操作,因为数据会持续到 2017 年等等。

我正在考虑一个快速解决方案,直到我开发出一个适合移动假期的预测。谁能指出我正确的方向?

我尝试过类似的方法,但这不起作用,因为我只需要这 3 周的滞后/领先:

df1 <- 
df1 %>%
mutate(New_Visitors = ifelse(Holiday == "Thanksgiving", lag(Visitors, (WKN - WKN_2015), Visitors)

逻辑:查找每年的感恩节,看看 WKN 是否匹配。如果不这样做,则根据 WKN 之间的差异调整从感恩节开始的接下来 3 周的访客。如果 WKN-WKN_2015 == -1 则在接下来的 3 行中领先访问者 1,如果 WKN-WKN_2015 == 1 则在接下来的 3 行中落后访问者 1

数据
df1 <- structure(list(Date = c("2016-11-06", "2016-11-13", "2016-11-20", 
"2016-11-27", "2016-12-04", "2016-12-11", "2017-11-23"), City = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L), .Label = "New York", class = "factor"), 
    Visitors = c(40000L, 50000L, 50000L, 100000L, 100000L, 70000L, 
    120000L), WKN = c(45L, 46L, 47L, 48L, 49L, 50L, 47L), WKN_2015 = c(46L, 
    47L, 48L, 49L, 50L, 51L, 47L), Holiday = structure(c(2L, 
    2L, 3L, 1L, 2L, 2L, 3L), .Label = c("Cyber_Monday", "No_Holiday", 
    "Thanksgiving"), class = "factor")), .Names = c("Date", "City", 
"Visitors", "WKN", "WKN_2015", "Holiday"), row.names = c(NA, 
7L), class = "data.frame")

【问题讨论】:

  • 不太明白你的预测在做什么,但你可以用%&lt;&gt;%(往复管)说df1 &lt;- df1 %&gt;% mutate(...)df1 %&lt;&gt;% mutate(...)
  • @NathanDay 每年查找感恩节,看看 WKN 是否匹配。如果不这样做,则根据 WKN 之间的差异从感恩节开始调整接下来的 3 周。如果 WKN-WKN_2015 == -1 则领先访客 1 位,如果 WKN-WKN_2015 == 1 则在接下来的 3 行中落后访客 1 位。
  • valueVolume 来自哪里?
  • @NathanDay 对不起我的错误刚刚编辑了它。

标签: r dataframe dplyr plyr


【解决方案1】:

您每年只对三周感兴趣,您可以计算“感恩节”行中的滞后值。我认为不需要dplyr

df1$New_Visitors <- df1$Visitors             # copy Visitors
ind <- which(df1$Holiday == "Thanksgiving")  # get number of "Thanksgiving" rows

invisible(sapply(ind, function(x) {
  lag <- df1[x, "WKN_2015"] - df1[x, "WKN"]       # calculate the lag
  df1[x:(x+2), "New_Visitors"] <<- df1[(x+lag):(x+lag+2), "Visitors"]  # rewrite
}))
> df1  # this method treats the three weeks as a unit, so made two NA rows in the example data)
        Date     City Visitors WKN WKN_2015      Holiday New_Visitors
1 2016-11-06 New York    40000  45       46   No_Holiday        40000
2 2016-11-13 New York    50000  46       47   No_Holiday        50000
3 2016-11-20 New York    50000  47       48 Thanksgiving       100000
4 2016-11-27 New York   100000  48       49 Cyber_Monday       100000
5 2016-12-04 New York   100000  49       50   No_Holiday        70000
6 2016-12-11 New York    70000  50       51   No_Holiday        70000
7 2017-11-23 New York   120000  47       47 Thanksgiving       120000
8       <NA>     <NA>       NA  NA       NA         <NA>           NA
9       <NA>     <NA>       NA  NA       NA         <NA>           NA

【讨论】:

    猜你喜欢
    • 2018-09-01
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    • 2022-01-17
    相关资源
    最近更新 更多