gpt4 book ai didi

r - 标记 R 中的 X 次先前出现

转载 作者:行者123 更新时间:2023-12-05 01:22:32 25 4
gpt4 key购买 nike

我有一个包含许多不同社交媒体创建者 (creator_id) 的数据集。他们发布了很多次 (posting_count),如果 ad = 1,则帖子被归类为 ad。现在我总是想将 ad = 1 之前的 3 个以前的帖子归类为 1。基本上,“goal_variable”是我想要得到的。没有循环的解决方案会很酷!!

creator_id <-c("aaa","aaa","aaa","aaa","aaa","aaa","aaa","aaa","bbb","bbb","bbb","bbb","bbb","bbb","bbb","bbb","bbb") 
posting_count <- c(143,144,145,146,147,148,149,150,90,91,92,93,94,95,96,97,98)
ad <- c(0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,1)
goal_variable <- c(0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,0)
df <- cbind(creator_id, posting_count, ad, goal_variable)

最佳答案

这是使用 map 的编程方式。基本上,对于每一行,检查当前行是否在最近的 ad == 1 之前的 3 到 1 个位置之间。

library(purrr)
library(dplyr)
df %>%
group_by(creator_id) %>%
mutate(goal_variable = map_int(row_number(), ~ any((.x - which(ad == 1)) %in% -3:-1)))

输出

# A tibble: 17 × 4
# Groups: creator_id [2]
creator_id posting_count ad goal_variable
<chr> <dbl> <dbl> <int>
1 aaa 143 0 0
2 aaa 144 0 0
3 aaa 145 0 0
4 aaa 146 0 1
5 aaa 147 0 1
6 aaa 148 0 1
7 aaa 149 1 0
8 aaa 150 0 0
9 bbb 90 0 0
10 bbb 91 0 0
11 bbb 92 0 0
12 bbb 93 0 1
13 bbb 94 0 1
14 bbb 95 0 1
15 bbb 96 1 1
16 bbb 97 0 1
17 bbb 98 1 0

关于r - 标记 R 中的 X 次先前出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73845959/

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