假設我們觀察一個視頻游戲玩家收集積分。每個觀察報告自上次訪問以來玩家收集了多少點。現在我想創建一個附加變數來指示用戶在最后 x(例如,90)秒內收集了多少點,包括當前觀察。
示例資料:
example_da = data.frame(time = c("2015-04-11 21:24:34", "2015-04-11 21:24:50", "2015-04-11 21:25:20", "2015-04-11 21:27:52", "2015-04-11 21:27:59", "2015-04-11 21:28:13",
"2015-04-11 21:30:06", "2015-04-11 21:31:05", "2015-04-11 21:31:47", "2015-04-11 21:38:01", "2015-04-11 21:39:05", "2015-04-11 21:40:06"),
points = c(2,3,1,6,2,5,1,1,3,5,2,4))
> example_da
time points
1 2015-04-11 21:24:34 2
2 2015-04-11 21:24:50 3
3 2015-04-11 21:25:20 1
4 2015-04-11 21:27:52 6
5 2015-04-11 21:27:59 2
6 2015-04-11 21:28:13 5
7 2015-04-11 21:30:06 1
8 2015-04-11 21:31:05 1
9 2015-04-11 21:31:47 3
10 2015-04-11 21:38:01 5
11 2015-04-11 21:39:05 2
12 2015-04-11 21:40:06 4
例如,對于觀察 3(“2015-04-11 21:25:20”),我們總結了“2015-04-11 21:24:34”(= 2)、“2015-04-11 21”的點:24:50" (=3), "2015-04-11 21:25:20" (=1),因為這些點都是在前 90 秒內收集的,因此我們的新變數“sum_points_preceding_90_seconds”得到 6 個點.
> target_da = data.frame(time = c("2015-04-11 21:24:34", "2015-04-11 21:24:50", "2015-04-11 21:25:20", "2015-04-11 21:27:52", "2015-04-11 21:27:59", "2015-04-11 21:28:13",
"2015-04-11 21:30:06", "2015-04-11 21:31:05", "2015-04-11 21:31:47", "2015-04-11 21:38:01", "2015-04-11 21:39:05", "2015-04-11 21:40:06"),
points = c(2,3,1,6,2,5,1,1,3,5,2,4),
sum_points_preceding_90_seconds = c(2, 5, 6, 6, 8, 13, 1, 2, 5, 5, 7, 6))
>
>
> target_da
time points sum_points_preceding_90_seconds
1 2015-04-11 21:24:34 2 2
2 2015-04-11 21:24:50 3 5
3 2015-04-11 21:25:20 1 6
4 2015-04-11 21:27:52 6 6
5 2015-04-11 21:27:59 2 8
6 2015-04-11 21:28:13 5 13
7 2015-04-11 21:30:06 1 1
8 2015-04-11 21:31:05 1 2
9 2015-04-11 21:31:47 3 5
10 2015-04-11 21:38:01 5 5
11 2015-04-11 21:39:05 2 7
12 2015-04-11 21:40:06 4 6
uj5u.com熱心網友回復:
您可以使用滑塊包執行此操作slide_index_sum()。它允許您指定一個索引,然后在該索引的每個元素之前或之后創建邊界以生成滑動視窗。
我認為您的預期結果可能存在錯誤2015-04-11 21:31:47?看起來它應該導致 4 而不是 5?
您可能需要before根據您的具體要求進行調整。
library(slider)
library(dplyr)
example_da <- tibble(
time = c(
"2015-04-11 21:24:34", "2015-04-11 21:24:50", "2015-04-11 21:25:20",
"2015-04-11 21:27:52", "2015-04-11 21:27:59", "2015-04-11 21:28:13",
"2015-04-11 21:30:06", "2015-04-11 21:31:05", "2015-04-11 21:31:47",
"2015-04-11 21:38:01", "2015-04-11 21:39:05", "2015-04-11 21:40:06"),
points = c(2,3,1,6,2,5,1,1,3,5,2,4)
)
example_da <- mutate(example_da, time = as.POSIXct(time, "UTC"))
# The current time 89 seconds before it = 90 seconds total
example_da <- example_da %>%
mutate(
sum_points_preceding_90_seconds =
slide_index_sum(
x = points,
i = time,
before = 89
)
)
example_da
#> # A tibble: 12 × 3
#> time points sum_points_preceding_90_seconds
#> <dttm> <dbl> <dbl>
#> 1 2015-04-11 21:24:34 2 2
#> 2 2015-04-11 21:24:50 3 5
#> 3 2015-04-11 21:25:20 1 6
#> 4 2015-04-11 21:27:52 6 6
#> 5 2015-04-11 21:27:59 2 8
#> 6 2015-04-11 21:28:13 5 13
#> 7 2015-04-11 21:30:06 1 1
#> 8 2015-04-11 21:31:05 1 2
#> 9 2015-04-11 21:31:47 3 4
#> 10 2015-04-11 21:38:01 5 5
#> 11 2015-04-11 21:39:05 2 7
#> 12 2015-04-11 21:40:06 4 6
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/460984.html
下一篇:基于其他列中的共同值的重復行?
