gpt4 book ai didi

r - 带有 order_by 和 with_order 的 dplyr 窗口函数

转载 作者:行者123 更新时间:2023-12-04 10:51:14 26 4
gpt4 key购买 nike

背景

dplyr 有窗口函数。当你想控制窗口函数的顺序时,
您可以使用 order_by .

数据

mydf <- data.frame(id = c("ana", "bob", "caroline",
"bob", "ana", "caroline"),
order = as.POSIXct(c("2015-01-01 18:00:00", "2015-01-01 18:05:00",
"2015-01-01 19:20:00", "2015-01-01 09:07:00",
"2015-01-01 08:30:00", "2015-01-01 11:11:00"),
format = "%Y-%m-%d %H:%M:%S"),
value = runif(6, 10, 20),
stringsAsFactors = FALSE)

# id order value
#1 ana 2015-01-01 18:00:00 19.00659
#2 bob 2015-01-01 18:05:00 13.64010
#3 caroline 2015-01-01 19:20:00 12.08506
#4 bob 2015-01-01 09:07:00 14.40996
#5 ana 2015-01-01 08:30:00 17.45165
#6 caroline 2015-01-01 11:11:00 14.50865

假设您要使用 lag() ,您可以执行以下操作。
arrange(mydf, id, order) %>%
group_by(id) %>%
mutate(check = lag(value))

# id order value check
#1 ana 2015-01-01 08:30:00 17.45165 NA
#2 ana 2015-01-01 18:00:00 19.00659 17.45165
#3 bob 2015-01-01 09:07:00 14.40996 NA
#4 bob 2015-01-01 18:05:00 13.64010 14.40996
#5 caroline 2015-01-01 11:11:00 14.50865 NA
#6 caroline 2015-01-01 19:20:00 12.08506 14.50865

但是,您可以避免使用 arrange()order_by() .
group_by(mydf, id) %>%
mutate(check = lag(value, order_by = order))

# id order value check
#1 ana 2015-01-01 18:00:00 19.00659 17.45165
#2 bob 2015-01-01 18:05:00 13.64010 14.40996
#3 caroline 2015-01-01 19:20:00 12.08506 14.50865
#4 bob 2015-01-01 09:07:00 14.40996 NA
#5 ana 2015-01-01 08:30:00 17.45165 NA
#6 caroline 2015-01-01 11:11:00 14.50865 NA

实验

我想对我想要的情况应用相同的程序
将行号分配给新列。使用示例数据,您可以执行以下操作。
group_by(mydf, id) %>%
arrange(order) %>%
mutate(num = row_number())

# id order value num
#1 ana 2015-01-01 08:30:00 17.45165 1
#2 ana 2015-01-01 18:00:00 19.00659 2
#3 bob 2015-01-01 09:07:00 14.40996 1
#4 bob 2015-01-01 18:05:00 13.64010 2
#5 caroline 2015-01-01 11:11:00 14.50865 1
#6 caroline 2015-01-01 19:20:00 12.08506 2

我们可以省略排列线吗?看到CRAN手册,我做了以下事情。
两次尝试都没有成功。
### Not working
group_by(mydf, id) %>%
mutate(num = row_number(order_by = order))

### Not working
group_by(mydf, id) %>%
mutate(num = order_by(order, row_number()))

我们怎样才能做到这一点?

最佳答案

我不是故意要自己回答这个问题的。但是,我决定分享
我发现我没有看到很多帖子使用 order_by尤其是with_order .我的答案是使用 with_order()而不是 order_by() .

group_by(mydf, id) %>%
mutate(num = with_order(order_by = order, fun = row_number, x = order))

# id order value num
#1 ana 2015-01-01 18:00:00 19.00659 2
#2 bob 2015-01-01 18:05:00 13.64010 2
#3 caroline 2015-01-01 19:20:00 12.08506 2
#4 bob 2015-01-01 09:07:00 14.40996 1
#5 ana 2015-01-01 08:30:00 17.45165 1
#6 caroline 2015-01-01 11:11:00 14.50865 1

我想看看两者有没有什么区别
在速度方面接近。在这种情况下,它们似乎非常相似。
library(microbenchmark)

mydf2 <- data.frame(id = rep(c("ana", "bob", "caroline",
"bob", "ana", "caroline"), times = 200000),
order = seq(as.POSIXct("2015-03-01 18:00:00", format = "%Y-%m-%d %H:%M:%S"),
as.POSIXct("2015-01-01 18:00:00", format = "%Y-%m-%d %H:%M:%S"),
length.out = 1200000),
value = runif(1200000, 10, 20),
stringsAsFactors = FALSE)

jazz1 <- function() {group_by(mydf2, id) %>%
arrange(order) %>%
mutate(num = row_number())}

jazz2 <- function() {group_by(mydf2, id) %>%
mutate(num = with_order(order_by = order, fun = row_number, x = order))}


res <- microbenchmark(jazz1, jazz2, times = 1000000L)
res

#Unit: nanoseconds
# expr min lq mean median uq max neval cld
# jazz1 32 36 47.17647 38 47 12308 1e+06 a
# jazz2 32 36 47.02902 38 47 12402 1e+06 a

关于r - 带有 order_by 和 with_order 的 dplyr 窗口函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28537437/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com