gpt4 book ai didi

r - 创建事件的累积计数并保留每个事件前后的第一年

转载 作者:行者123 更新时间:2023-12-05 04:27:59 24 4
gpt4 key购买 nike

我有一个纵向数据集,其中包含个人以及有关他们当前居住地的信息。下面的代码创建了一个示例 df:

set.seed(123)
df <- tibble(
id = c(1, 2, 3, 4, 5,
1, 2, 3, 5, 6, 7,
2, 3, 4, 6, 7, 8,
1, 2, 3, 4, 6, 7, 8
),
year = c(rep(2009, 5),
rep(2010, 6),
rep(2011, 6),
rep(2012, 7)),
age = c(0, 0, 0, 0, 0,
1, 1, 1, 1, 0, 1,
2, 2, 2, 1, 2, 2,
3, 3, 3, 3, 2, 3, 3),
town = c("0", "0", "2", "0", "0",
"1", "2", "1", "3", "0", "1",
"3", "1", "4", "1", "2", "1",
"4", "2", "2", "1", "2", "1", "5")
)

我对搬家的原因很感兴趣(例如,收入、受教育程度、家庭结构等是否对你是否搬家有影响,以及它是否会影响你搬家的地区),所以我编码了事件,“移动”,以及使用以下代码的“flag_first_move”:

df3 <- df %>%
arrange(year, id) %>%
group_by(id) %>%
mutate(first_year = min(year)) %>%
mutate(first_town = list(town[year==first_year])) %>%
mutate(flag_move = as.numeric(year != first_year & !(town %in% unlist(first_town)) & town !="")) %>%
mutate(flag_first_move = (flag_move==1 & as.numeric(!duplicated(flag_move)))) %>%
mutate(moved = case_when(town !=lag(town) ~ 1,
TRUE ~ 0)) %>%
mutate(flag_cum_move = (cumsum(c(0, diff(moved)) !=0) + 1)) #This doesn't work as intended

“flag_first_move”给我移动的第一个事件。每次移动时,“移动”都会给我一个标志。最后,通过尝试创建变量“flag_cum_move”,我想要每个事件的累积计数(这样每次一个人移动它都会加 1)——我不知道该怎么做!

最后,我想看看每个人在每个事件(移动)前后的一年。这是我试图完成此任务的代码:

df4 <- df3 %>%
group_by(id) %>%
filter(any(flag_first_move == 1)) %>%
mutate(
year_before = ifelse(
between(year[moved == 1] - year, 1, 1), 1, 0),
year_after = ifelse(
between(year - year[moved == 1], 1, 1), 1, 0),
)

它在只发生一个事件的情况下工作正常,但在每年发生多个事件的情况下,它会向我发出“year_after”变量的警告,而且我也没有得到预期的结果.我不明白为什么。

最佳答案

看看这是不是你想要的。最好能在问题中显示预期的输出,所以如果我误解了什么,请在下面的评论中指出,我可以相应地进行调整。

library(tidyverse)

df <- tibble(
id = c(
1, 2, 3, 4, 5,
1, 2, 3, 5, 6, 7,
2, 3, 4, 6, 7, 8,
1, 2, 3, 4, 6, 7, 8
),
year = c(
rep(2009, 5),
rep(2010, 6),
rep(2011, 6),
rep(2012, 7)
),
age = c(
0, 0, 0, 0, 0,
1, 1, 1, 1, 0, 1,
2, 2, 2, 1, 2, 2,
3, 3, 3, 3, 2, 3, 3
),
town = c(
"0", "0", "2", "0", "0",
"1", "2", "1", "3", "0", "1",
"3", "1", "4", "1", "2", "1",
"4", "2", "2", "1", "2", "1", "5"
)
)

df3 <- df %>%
arrange(id, year) %>%
group_by(id) %>%
mutate(
first_year = min(year),
first_town = if_else(year == first_year, town, NA_character_)
) %>%
fill(first_town) %>%
mutate(
flag_move = if_else(year != first_year & town != first_town, 1, 0),
flag_first_move = if_else(cumsum(flag_move) == 1, 1, 0),
moved = if_else(town != lag(town), 1, 0)
) %>%
replace_na(list(moved = 0)) %>%
mutate(flag_cum_move = cumsum(moved))

df4 <- df3 %>%
filter(any(flag_first_move == 1)) %>%
mutate(
year_before = if_else(lead(moved) == 1, 1, 0),
year_after = if_else(lag(moved) == 1, 1, 0)
) %>%
ungroup()

df4
#> # A tibble: 24 × 12
#> id year age town first_year first_town flag_move flag_first_move moved
#> <dbl> <dbl> <dbl> <chr> <dbl> <chr> <dbl> <dbl> <dbl>
#> 1 1 2009 0 0 2009 0 0 0 0
#> 2 1 2010 1 1 2009 0 1 1 1
#> 3 1 2012 3 4 2009 0 1 0 1
#> 4 2 2009 0 0 2009 0 0 0 0
#> 5 2 2010 1 2 2009 0 1 1 1
#> 6 2 2011 2 3 2009 0 1 0 1
#> 7 2 2012 3 2 2009 0 1 0 1
#> 8 3 2009 0 2 2009 2 0 0 0
#> 9 3 2010 1 1 2009 2 1 1 1
#> 10 3 2011 2 1 2009 2 1 0 0
#> # … with 14 more rows, and 3 more variables: flag_cum_move <dbl>,
#> # year_before <dbl>, year_after <dbl>

reprex package 创建于 2022-06-22 (v2.0.1)

关于r - 创建事件的累积计数并保留每个事件前后的第一年,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72712106/

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