在我的資料集中,我有以下變數:
- gid = 單元識別符號
- 年
- 戰斗:每年計數
- 發生率:如果那一年在那個牢房里發生了至少一場戰斗。 對于關聯變數的構造,我使用了以下代碼: test$IncidenceBattles <-ifelse(Test$Battles>= 1,c(1), c(0))
我想創建一個二進制變數 OnsetBattle,如果我們在特定年份觀察到至少 1 場戰斗并且在前一年沒有觀察到,則它等于 1。
2001 年單元格 115593 的示例。 OnsetBattle 將等于 1,因為 2001 年發生戰斗 = 1,并且 2000 年沒有戰斗。
注意:如果有缺失值也沒關系。尤其是在 1997 年之前。
我的資料集的子集:
structure(list(gid = c(115593, 115593, 115593, 115593, 115593,
115593, 115593, 115593, 115593, 115593, 115593, 115593, 115593,
115593, 115593), Year = c(1996, 1997, 1998, 1999, 2000, 2001,
2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010), Battles = c(NA,
7, 9, 291, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0), IncidenceBattles = c(NA,
1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -15L), groups = structure(list(
gid = 115593, .rows = structure(list(1:15), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L), .drop = TRUE))
uj5u.com熱心網友回復:
df <- data.frame(gid = c(115593, 115593, 115593, 115593, 115593, 115593, 115593,
115593, 115593, 115593, 115593, 115593, 115593, 115593, 115593),
Year = c(1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
2005, 2006, 2007, 2008, 2009, 2010),
Battles = c(NA, 7, 9, 291, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0),
IncidenceBattles = c(NA, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0))
gid = 115593, .rows = structure(list(1:15), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
# find years with no battles ), row.names = c(NA, -1L), .drop = TRUE))
idx <- which((df$Battles == 0) & (df$IncidenceBattles == 0))
# ignore first and last rows
idx <- setdiff(idx, c(1, nrow(df)))
# move the index forward 1 year
idx <- idx 1
# check the year after no-battle years to see if there are any battles
idx2 <- ((df[ idx, c('Battles', 'IncidenceBattles') ] |> rowSums()) > 0) |> which()
# retain years that have battles
idx <- idx[ idx2 ]
# initialize the variable to 0
df$OnsetBattle <- 0
# set to 1 for years with battles
df[ idx, 'OnsetBattle' ] <- 1
print(df)
uj5u.com熱心網友回復:
通過對我的發生變數應用滯后得到它
df %>%
ungroup() %>%
arrange(gid, Year) %>%
group_by(gid) %>%
mutate(lag_battles = Lag(IncidenceBattles, 1),
OnsetBattle = ifelse(IncidenceBattles==1 & lag_battles==0, 1, 0))
uj5u.com熱心網友回復:
取 IncidenceBattle 值的差,如果等于 1,則使用 1。注意 IncidenceBattles 可以定義為 sign(Battles)。該問題沒有指定如何處理前兩行,因此我們使用了 NA。
library(dplyr)
d %>%
arrange(gid, Year) %>%
group_by(gid) %>%
mutate(OnsetBattle = c(NA, (diff(IncidenceBattles) == 1))) %>%
ungroup
給予:
# A tibble: 15 × 5
gid Year Battles IncidenceBattles OnsetBattle
<dbl> <dbl> <dbl> <dbl> <int>
1 115593 1996 NA NA NA
2 115593 1997 7 1 NA
3 115593 1998 9 1 0
4 115593 1999 291 1 0
5 115593 2000 0 0 0
6 115593 2001 5 1 1
7 115593 2002 0 0 0
8 115593 2003 0 0 0
9 115593 2004 0 0 0
10 115593 2005 0 0 0
11 115593 2006 0 0 0
12 115593 2007 0 0 0
13 115593 2008 0 0 0
14 115593 2009 0 0 0
15 115593 2010 0 0 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/521629.html
標籤:r二进制条件语句
