dplyr如何按组落后 [英] dplyr how to lag by group

查看:70
本文介绍了dplyr如何按组落后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个订单和应收账款的数据框,其中包含交货时间。
是否可以根据组提前期使用dplyr填写接收列?

I have a data frame of orders and receivables with lead times. Can I use dplyr to fill in the receive column according to the groups lead time?

df <- data.frame(team = c("a","a","a","a", "a", "b", "b", "b", "b", "b"),
                 order     = c(2, 4, 3, 5, 6, 7, 8, 5, 4, 5),
                 lead_time = c(3, 3, 3, 3, 3, 2, 2, 2, 2, 2))
>df
   team order lead_time
     a     2         3
     a     4         3
     a     3         3
     a     5         3
     a     6         3
     b     7         2
     b     8         2
     b     5         2
     b     4         2
     b     5         2

并添加如下所示的接收列:

And adding a receive column like so:

dfb <- data.frame(team = c("a","a","a","a", "a", "b", "b", "b", "b", "b"),
                 order     = c(2, 4, 3, 5, 6, 7, 8, 5, 4, 5),
                 lead_time = c(3, 3, 3, 3, 3, 2, 2, 2, 2, 2), 
                 receive = c(0, 0, 0, 2, 4, 0, 0, 7, 8, 5))

>dfb
team order lead_time receive
    a     2         3       0
    a     4         3       0
    a     3         3       0
    a     5         3       2
    a     6         3       4
    b     7         2       0
    b     8         2       0
    b     5         2       7
    b     4         2       8
    b     5         2       5

我一直在思考,但遇到错误

I was thinking along these lines but run into an error

dfc <- df %>%
      group_by(team) %>%
      mutate(receive = if_else( row_number() < lead_time, 0, lag(order, n = lead_time)))

Error in mutate_impl(.data, dots) : 
  could not convert second argument to an integer. type=SYMSXP, length = 1

感谢您的帮助!

推荐答案

这看起来像是个错误; dplyr stats lag 函数可能会有一些意外的掩盖$ c>包,请尝试以下解决方法:

This looks like a bug; There might be some unintended mask of the lag function between dplyr and stats package, try this work around:

df %>% 
    group_by(team) %>% 
    # explicitly specify the source of the lag function here
    mutate(receive = dplyr::lag(order, n=unique(lead_time), default=0))

#Source: local data frame [10 x 4]
#Groups: team [2]

#     team order lead_time receive
#   <fctr> <dbl>     <dbl>   <dbl>
#1       a     2         3       0
#2       a     4         3       0
#3       a     3         3       0
#4       a     5         3       2
#5       a     6         3       4
#6       b     7         2       0
#7       b     8         2       0
#8       b     5         2       7
#9       b     4         2       8
#10      b     5         2       5

这篇关于dplyr如何按组落后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆