【问题标题】:Generating a order rank column with dplyr based on changes on the grouping variable根据分组变量的变化使用 dplyr 生成订单排名列
【发布时间】:2016-01-22 18:21:56
【问题描述】:

我在使用 dplyr 生成排名列时遇到了一点挑战 在特定消费者的事务日志中的 tbl_df 对象上。我的数据如下所示:

                                        consumerid merchant_id      eventtimestamp merchant_visit_rank
                                              (chr)       (int)              (time)          (dbl)
            1  004a5cc3-3d60-4d14-85b3-706e454aae13          52 2015-01-15 13:33:00              0
            2  004a5cc3-3d60-4d14-85b3-706e454aae13          56 2015-01-16 13:58:03              1
            3  004a5cc3-3d60-4d14-85b3-706e454aae13          56 2015-01-16 13:58:41              0
            4  004a5cc3-3d60-4d14-85b3-706e454aae13          52 2015-01-16 13:59:05              1
            5  004a5cc3-3d60-4d14-85b3-706e454aae13          52 2015-01-16 13:59:55              1
            6  004a5cc3-3d60-4d14-85b3-706e454aae13          52 2015-01-16 14:15:56              0
            7  004a5cc3-3d60-4d14-85b3-706e454aae13          58 2015-01-21 13:52:18              1
            8  004a5cc3-3d60-4d14-85b3-706e454aae13          58 2015-01-21 13:52:19              0
            9  004a5cc3-3d60-4d14-85b3-706e454aae13          54 2015-01-21 13:52:24              0
            10 004a5cc3-3d60-4d14-85b3-706e454aae13          58 2015-01-21 13:52:29              0
            ..                                  ...         ...                 ...            ...

我想生成一个商家访问排名,以便告诉我此商家在此交易中的顺序 会议。在我们的例子中,正确的排名应该是:

                                        consumerid merchant_id      eventtimestamp merchant_visit_rank
                                              (chr)       (int)              (time)          (dbl)
            1  004a5cc3-3d60-4d14-85b3-706e454aae13          52 2015-01-15 13:33:00              1
            2  004a5cc3-3d60-4d14-85b3-706e454aae13          56 2015-01-16 13:58:03              2
            3  004a5cc3-3d60-4d14-85b3-706e454aae13          56 2015-01-16 13:58:41              2
            4  004a5cc3-3d60-4d14-85b3-706e454aae13          52 2015-01-16 13:59:05              3
            5  004a5cc3-3d60-4d14-85b3-706e454aae13          52 2015-01-16 13:59:55              3
            6  004a5cc3-3d60-4d14-85b3-706e454aae13          52 2015-01-16 14:15:56              3
            7  004a5cc3-3d60-4d14-85b3-706e454aae13          58 2015-01-21 13:52:18              4
            8  004a5cc3-3d60-4d14-85b3-706e454aae13          58 2015-01-21 13:52:19              4
            9  004a5cc3-3d60-4d14-85b3-706e454aae13          54 2015-01-21 13:52:24              5
            10 004a5cc3-3d60-4d14-85b3-706e454aae13          58 2015-01-21 13:52:29              6
            ..                                  ...         ...                 ...            ...

我曾尝试像这样使用 dplyr 中的窗口函数:

            measure_media_interaction %>% 
              #selecting the fields we wish from the dataframe
              select(consumerid,merchant_id,eventtimestamp) %>%
              #mutate a placeholder column to be used for the rank 
              mutate(merchant_visit = 0) %>% 
              #sort them by consumer and timestamp
              arrange(consumerid,eventtimestamp) %>%
              #change the column so it shows that this merchant was the first this consumer visited 
              #or not 
              mutate(merchant_visit = 
                       ifelse(lead(merchant_id)!=merchant_id,merchant_visit,merchant_visit+1))

但是我被卡住了,我不知道如何有效地做到这一点。对此有什么想法吗?

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    这里有一个解决方案。我们使用lag 来测试merchant_id 是否发生变化,使用cumsum 来增加计数器。

    measure_media_interaction %>% 
      select(consumerid,merchant_id,eventtimestamp) %>%
      arrange(consumerid,eventtimestamp) %>%
      mutate(merchant_visit=cumsum(c(1,(merchant_id != lag(merchant_id))[-1])))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 2014-11-24
      • 2016-11-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多