r语言 - 如果某些列在时间序列df中相互匹配(通过组id),如何创建虚拟?



我有一个包含每位女性生育史的df。每个女性(即id_mdob)有18行,因为我从过去18年开始重建她的出生情况。

我要做的是为birth_in_year生成一个虚拟变量,地点:

  • 1。)如果date_year在nchild1-nchild10中,则1
  • 2。)如果nchild1-nchild10中的date_year并且在nchild1-nchild10列中存在匹配的年份(即同年出生的孩子),则2

例如,对于id_mdob_sy (11323082111985 2011), nchild1和nchild2是2001,所以我想要birth_in_year

与date_year=2001在同一行中为2,与date_year=1995在同一行中为1(其余为0)。我还想要prev_parity(即孩子的数量)在第一个孩子出生的年份变成1(在这种情况下,1995年),当2011年其他两个孩子出生时变成3(3因为总共有3个孩子,一个出生在1995年,两个出生在2001年)。

## what I have now (id_mdob_sy: 11323082111985 2011): 
survey_year date_year   prev_parity birth_in_year nchild1 nchild2 nchild3
2011        2011          2              0          2001    2001    1995
2011        2010          2              0          2001    2001    1995
2011        2009          2              0          2001    2001    1995
2011        2008          2              0          2001    2001    1995
2011        2007          2              0          2001    2001    1995
2011        2006          2              0          2001    2001    1995
2011        2005          2              0          2001    2001    1995
2011        2004          2              0          2001    2001    1995
2011        2003          2              0          2001    2001    1995
2011        2002          2              0          2001    2001    1995
2011        2001          1              1          2001    2001    1995
2011        2000          1              0          2001    2001    1995
2011        1999          1              0          2001    2001    1995
2011        1998          1              0          2001    2001    1995
2011        1997          1              0          2001    2001    1995
2011        1996          1              0          2001    2001    1995
2011        1995          0              1          2001    2001    1995
2011        1994          0              0          2001    2001    1995
## desired df
survey_year date_year   prev_parity birth_in_year nchild1 nchild2 nchild3
2011        2011          3              0          2001    2001    1995
2011        2010          3              0          2001    2001    1995
2011        2009          3              0          2001    2001    1995
2011        2008          3              0          2001    2001    1995
2011        2007          3              0          2001    2001    1995
2011        2006          3              0          2001    2001    1995
2011        2005          3              0          2001    2001    1995
2011        2004          3              0          2001    2001    1995
2011        2003          3              0          2001    2001    1995
2011        2002          3              0          2001    2001    1995
2011        2001          1              2          2001    2001    1995
2011        2000          1              0          2001    2001    1995
2011        1999          1              0          2001    2001    1995
2011        1998          1              0          2001    2001    1995
2011        1997          1              0          2001    2001    1995
2011        1996          1              0          2001    2001    1995
2011        1995          0              1          2001    2001    1995
2011        1994          0              0          2001    2001    1995 

到目前为止,我已经尝试使用ifelse编写代码,但是我只能让birth_in_year捕获第一部分(如果date_year在nchild1-nchild10==1)。我有麻烦弄清楚如何同时指出在同一变量(birth_in_year),如果date_year在nchild1-nchild10==1;nchild1-nchild10中的date_year和nchild1-nchild10中的年份匹配==2。抱歉,这段代码太长了:

test <- pdata_08 %>% 
rowwise() %>%
mutate(birth_in_year = ifelse(date_year %in% c(nchild1, nchild2, nchild3, nchild4, nchild5, nchild6, nchild7, nchild8, nchild9, nchild10), 1, 0) & (nchild1 == nchild2 & !is.na(nchild1) & !is.na(nchild2)), 2, 0) | (nchild2 == nchild3 & !is.na(nchild2) & !is.na(nchild3)), 2, 0) | (nchild3 == nchild4 & !is.na(nchild3) & !is.na(nchild4)), 2, 0) | (nchild4 == nchild5 & !is.na(nchild4) & !is.na(nchild5)), 2, 0) | (nchild6 == nchild7 & !is.na(nchild6) & !is.na(nchild7)), 2, 0) | (nchild7 == nchild8 & !is.na(nchild7) & !is.na(nchild8)), 2, 0) | (nchild8 == nchild9 & !is.na(nchild8) & !is.na(nchild8)), 2, 0) | (nchild9 == nchild10 & !is.na(nchild9) & !is.na(nchild10)), 2, 0) ) %>%
group_by(id_mdob) %>%
mutate(prev_parity = cumsum(birth_in_year))

下面是df:

的前75行
structure(list(id_mdob_sy = c("111990 2018", "111990 2018", "111990 2018", 
"111990 2018", "111990 2018", "111990 2018", "111990 2018", "111990 2018", 
"111990 2018", "111990 2018", "111990 2018", "111990 2018", "111990 2018", 
"111990 2018", "111990 2018", "111990 2018", "111990 2018", "111990 2018", 
"221995 2018", "221995 2018", "221995 2018", "221995 2018", "221995 2018", 
"221995 2018", "221995 2018", "221995 2018", "221995 2018", "221995 2018", 
"221995 2018", "221995 2018", "221995 2018", "221995 2018", "221995 2018", 
"221995 2018", "221995 2018", "221995 2018", "311980 2018", "311980 2018", 
"311980 2018", "311980 2018", "311980 2018", "311980 2018", "311980 2018", 
"311980 2018", "311980 2018", "311980 2018", "311980 2018", "311980 2018", 
"311980 2018", "311980 2018", "311980 2018", "311980 2018", "311980 2018", 
"311980 2018", "321988 2015", "321988 2015", "321988 2015", "321988 2015", 
"321988 2015", "321988 2015", "321988 2015", "321988 2015", "321988 2015", 
"321988 2015", "321988 2015", "321988 2015", "321988 2015", "321988 2015", 
"321988 2015", "321988 2015", "321988 2015", "321988 2015", "321991 2017", 
"321991 2017", "321991 2017"), id_mdob = c(111990, 111990, 111990, 
111990, 111990, 111990, 111990, 111990, 111990, 111990, 111990, 
111990, 111990, 111990, 111990, 111990, 111990, 111990, 221995, 
221995, 221995, 221995, 221995, 221995, 221995, 221995, 221995, 
221995, 221995, 221995, 221995, 221995, 221995, 221995, 221995, 
221995, 311980, 311980, 311980, 311980, 311980, 311980, 311980, 
311980, 311980, 311980, 311980, 311980, 311980, 311980, 311980, 
311980, 311980, 311980, 321988, 321988, 321988, 321988, 321988, 
321988, 321988, 321988, 321988, 321988, 321988, 321988, 321988, 
321988, 321988, 321988, 321988, 321988, 321991, 321991, 321991
), id = c(11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 
11, 11, 11, 11, 11, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 
22, 22, 22, 22, 22, 22, 22, 31, 31, 31, 31, 31, 31, 31, 31, 31, 
31, 31, 31, 31, 31, 31, 31, 31, 31, 32, 32, 32, 32, 32, 32, 32, 
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32), survey_date = structure(c(17532, 
17532, 17532, 17532, 17532, 17532, 17532, 17532, 17532, 17532, 
17532, 17532, 17532, 17532, 17532, 17532, 17532, 17532, 17532, 
17532, 17532, 17532, 17532, 17532, 17532, 17532, 17532, 17532, 
17532, 17532, 17532, 17532, 17532, 17532, 17532, 17532, 17532, 
17532, 17532, 17532, 17532, 17532, 17532, 17532, 17532, 17532, 
17532, 17532, 17532, 17532, 17532, 17532, 17532, 17532, 16467, 
16467, 16467, 16467, 16467, 16467, 16467, 16467, 16467, 16467, 
16467, 16467, 16467, 16467, 16467, 16467, 16467, 16467, 17167, 
17167, 17167), class = "Date"), survey_year = c(2018, 2018, 2018, 
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2015, 2015, 2015, 2015, 
2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 
2015, 2015, 2015, 2017, 2017, 2017), date_year = c(2018, 2017, 
2016, 2015, 2014, 2013, 2012, 2011, 2010, 2009, 2008, 2007, 2006, 
2005, 2004, 2003, 2002, 2001, 2018, 2017, 2016, 2015, 2014, 2013, 
2012, 2011, 2010, 2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 
2001, 2018, 2017, 2016, 2015, 2014, 2013, 2012, 2011, 2010, 2009, 
2008, 2007, 2006, 2005, 2004, 2003, 2002, 2001, 2015, 2014, 2013, 
2012, 2011, 2010, 2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 
2001, 2000, 1999, 1998, 2017, 2016, 2015), prev_parity = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0), birth_in_year = c(0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 1), mom_age = c(28, 27, 26, 25, 24, 23, 
22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 23, 22, 21, 20, 
19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 38, 37, 36, 
35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 26, 
25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 
9, 25, 24, 23), family500_year = c(1, 1, 1, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
1, 1, 0), prev_parity_1_plus = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 
1, 0), nchild1 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2015, 2015, 
2015), nchild2 = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
), nchild3 = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
), nchild4 = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
), nchild5 = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
), nchild6 = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_
), nchild7 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), 
nchild8 = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA), nchild9 = c(NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA), nchild10 = c(NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), educcat = c(3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2), educcat_college = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0), hh_income_net = c(14120, 
14120, 14120, 14120, 14120, 14120, 14120, 14120, 14120, 14120, 
14120, 14120, 14120, 14120, 14120, 14120, 14120, 14120, 5510, 
5510, 5510, 5510, 5510, 5510, 5510, 5510, 5510, 5510, 5510, 
5510, 5510, 5510, 5510, 5510, 5510, 5510, 8203.990234375, 
8203.990234375, 8203.990234375, 8203.990234375, 8203.990234375, 
8203.990234375, 8203.990234375, 8203.990234375, 8203.990234375, 
8203.990234375, 8203.990234375, 8203.990234375, 8203.990234375, 
8203.990234375, 8203.990234375, 8203.990234375, 8203.990234375, 
8203.990234375, 4800, 4800, 4800, 4800, 4800, 4800, 4800, 
4800, 4800, 4800, 4800, 4800, 4800, 4800, 4800, 4800, 4800, 
4800, 3410, 3410, 3410), hh_income_annual_usd = c(45184, 
45184, 45184, 45184, 45184, 45184, 45184, 45184, 45184, 45184, 
45184, 45184, 45184, 45184, 45184, 45184, 45184, 45184, 17632, 
17632, 17632, 17632, 17632, 17632, 17632, 17632, 17632, 17632, 
17632, 17632, 17632, 17632, 17632, 17632, 17632, 17632, 26252.76875, 
26252.76875, 26252.76875, 26252.76875, 26252.76875, 26252.76875, 
26252.76875, 26252.76875, 26252.76875, 26252.76875, 26252.76875, 
26252.76875, 26252.76875, 26252.76875, 26252.76875, 26252.76875, 
26252.76875, 26252.76875, 15360, 15360, 15360, 15360, 15360, 
15360, 15360, 15360, 15360, 15360, 15360, 15360, 15360, 15360, 
15360, 15360, 15360, 15360, 10912, 10912, 10912), hh_income_annual_log = c(10.7184983208529, 
10.7184983208529, 10.7184983208529, 10.7184983208529, 10.7184983208529, 
10.7184983208529, 10.7184983208529, 10.7184983208529, 10.7184983208529, 
10.7184983208529, 10.7184983208529, 10.7184983208529, 10.7184983208529, 
10.7184983208529, 10.7184983208529, 10.7184983208529, 10.7184983208529, 
10.7184983208529, 9.77747071195264, 9.77747071195264, 9.77747071195264, 
9.77747071195264, 9.77747071195264, 9.77747071195264, 9.77747071195264, 
9.77747071195264, 9.77747071195264, 9.77747071195264, 9.77747071195264, 
9.77747071195264, 9.77747071195264, 9.77747071195264, 9.77747071195264, 
9.77747071195264, 9.77747071195264, 9.77747071195264, 10.175526738648, 
10.175526738648, 10.175526738648, 10.175526738648, 10.175526738648, 
10.175526738648, 10.175526738648, 10.175526738648, 10.175526738648, 
10.175526738648, 10.175526738648, 10.175526738648, 10.175526738648, 
10.175526738648, 10.175526738648, 10.175526738648, 10.175526738648, 
10.175526738648, 9.63952200670166, 9.63952200670166, 9.63952200670166, 
9.63952200670166, 9.63952200670166, 9.63952200670166, 9.63952200670166, 
9.63952200670166, 9.63952200670166, 9.63952200670166, 9.63952200670166, 
9.63952200670166, 9.63952200670166, 9.63952200670166, 9.63952200670166, 
9.63952200670166, 9.63952200670166, 9.63952200670166, 9.29761838008324, 
9.29761838008324, 9.29761838008324), marital_stat = c(20, 
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
20, 20, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 
10, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 10, 10, 10), 
maritalcat = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 0, 0, 0), emp_stat = c(NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
3, 3, 3), disability_stat = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2), weight = c(1294.89001464844, 
1294.89001464844, 1294.89001464844, 1294.89001464844, 1294.89001464844, 
1294.89001464844, 1294.89001464844, 1294.89001464844, 1294.89001464844, 
1294.89001464844, 1294.89001464844, 1294.89001464844, 1294.89001464844, 
1294.89001464844, 1294.89001464844, 1294.89001464844, 1294.89001464844, 
1294.89001464844, 1294.89001464844, 1294.89001464844, 1294.89001464844, 
1294.89001464844, 1294.89001464844, 1294.89001464844, 1294.89001464844, 
1294.89001464844, 1294.89001464844, 1294.89001464844, 1294.89001464844, 
1294.89001464844, 1294.89001464844, 1294.89001464844, 1294.89001464844, 
1294.89001464844, 1294.89001464844, 1294.89001464844, 1975.36999511719, 
1975.36999511719, 1975.36999511719, 1975.36999511719, 1975.36999511719, 
1975.36999511719, 1975.36999511719, 1975.36999511719, 1975.36999511719, 
1975.36999511719, 1975.36999511719, 1975.36999511719, 1975.36999511719, 
1975.36999511719, 1975.36999511719, 1975.36999511719, 1975.36999511719, 
1975.36999511719, 1368, 1368, 1368, 1368, 1368, 1368, 1368, 
1368, 1368, 1368, 1368, 1368, 1368, 1368, 1368, 1368, 1368, 
1368, 1039, 1039, 1039), region = c(2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 26, 26, 26, 26, 26, 26, 26, 26, 
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 2, 2, 2), birth_country = c(616, 
616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 
616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 
616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 
616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 
616, 616, 616, 616, 616, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1), birth_citizenship = c(616, 
616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 
616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 
616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 
616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 
616, 616, 616, 616, 616, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1), residence = c(616, 616, 616, 
616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 
616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 
616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 
616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 616, 
616, 616, 616, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1), rural = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 1, 1, 1), stock_sq = c(0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1), mom_age_sq = c(784, 
729, 676, 625, 576, 529, 484, 441, 400, 361, 324, 289, 256, 
225, 196, 169, 144, 121, 529, 484, 441, 400, 361, 324, 289, 
256, 225, 196, 169, 144, 121, 100, 81, 64, 49, 36, 1444, 
1369, 1296, 1225, 1156, 1089, 1024, 961, 900, 841, 784, 729, 
676, 625, 576, 529, 484, 441, 676, 625, 576, 529, 484, 441, 
400, 361, 324, 289, 256, 225, 196, 169, 144, 121, 100, 81, 
625, 576, 529), survey_month = c("Jan 2018", "Jan 2018", 
"Jan 2018", "Jan 2018", "Jan 2018", "Jan 2018", "Jan 2018", 
"Jan 2018", "Jan 2018", "Jan 2018", "Jan 2018", "Jan 2018", 
"Jan 2018", "Jan 2018", "Jan 2018", "Jan 2018", "Jan 2018", 
"Jan 2018", "Jan 2018", "Jan 2018", "Jan 2018", "Jan 2018", 
"Jan 2018", "Jan 2018", "Jan 2018", "Jan 2018", "Jan 2018", 
"Jan 2018", "Jan 2018", "Jan 2018", "Jan 2018", "Jan 2018", 
"Jan 2018", "Jan 2018", "Jan 2018", "Jan 2018", "Jan 2018", 
"Jan 2018", "Jan 2018", "Jan 2018", "Jan 2018", "Jan 2018", 
"Jan 2018", "Jan 2018", "Jan 2018", "Jan 2018", "Jan 2018", 
"Jan 2018", "Jan 2018", "Jan 2018", "Jan 2018", "Jan 2018", 
"Jan 2018", "Jan 2018", "Feb 2015", "Feb 2015", "Feb 2015", 
"Feb 2015", "Feb 2015", "Feb 2015", "Feb 2015", "Feb 2015", 
"Feb 2015", "Feb 2015", "Feb 2015", "Feb 2015", "Feb 2015", 
"Feb 2015", "Feb 2015", "Feb 2015", "Feb 2015", "Feb 2015", 
"Jan 2017", "Jan 2017", "Jan 2017"), month_region = c(12, 
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 
12, 12, 12, 12, 12, 12, 12, 12, 226, 226, 226, 226, 226, 
226, 226, 226, 226, 226, 226, 226, 226, 226, 226, 226, 226, 
226, 12, 12, 12), month = c("01", "01", "01", "01", "01", 
"01", "01", "01", "01", "01", "01", "01", "01", "01", "01", 
"01", "01", "01", "01", "01", "01", "01", "01", "01", "01", 
"01", "01", "01", "01", "01", "01", "01", "01", "01", "01", 
"01", "01", "01", "01", "01", "01", "01", "01", "01", "01", 
"01", "01", "01", "01", "01", "01", "01", "01", "01", "02", 
"02", "02", "02", "02", "02", "02", "02", "02", "02", "02", 
"02", "02", "02", "02", "02", "02", "02", "01", "01", "01"
), newborn = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0)), row.names = c(NA, -75L), class = c("tbl_df", 
"tbl", "data.frame"))

很奇怪,你的dput对象的每个nchild都是0。我使用df与上面的表。

df <- read.table(text = "survey_year date_year   nchild1 nchild2 nchild3
2011        2011                  2001    2001    1995
2011        2010                  2001    2001    1995
2011        2009                  2001    2001    1995
2011        2008                  2001    2001    1995
2011        2007                  2001    2001    1995
2011        2006                  2001    2001    1995
2011        2005                  2001    2001    1995
2011        2004                  2001    2001    1995
2011        2003                  2001    2001    1995
2011        2002                  2001    2001    1995
2011        2001                  2001    2001    1995
2011        2000                  2001    2001    1995
2011        1999                  2001    2001    1995
2011        1998                  2001    2001    1995
2011        1997                  2001    2001    1995
2011        1996                  2001    2001    1995
2011        1995                  2001    2001    1995
2011        1994                  2001    2001    1995 ", header=T)

你可以试试

df %>%
mutate(birth_in_year = rowSums(date_year == select(., contains("nchild")))) %>%
arrange(date_year) %>%
mutate(prev_parity = cumsum(birth_in_year))
survey_year date_year nchild1 nchild2 nchild3 birth_in_year prev_parity
1         2011      1994    2001    2001    1995             0           0
2         2011      1995    2001    2001    1995             1           1
3         2011      1996    2001    2001    1995             0           1
4         2011      1997    2001    2001    1995             0           1
5         2011      1998    2001    2001    1995             0           1
6         2011      1999    2001    2001    1995             0           1
7         2011      2000    2001    2001    1995             0           1
8         2011      2001    2001    2001    1995             2           3
9         2011      2002    2001    2001    1995             0           3
10        2011      2003    2001    2001    1995             0           3
11        2011      2004    2001    2001    1995             0           3
12        2011      2005    2001    2001    1995             0           3
13        2011      2006    2001    2001    1995             0           3
14        2011      2007    2001    2001    1995             0           3
15        2011      2008    2001    2001    1995             0           3
16        2011      2009    2001    2001    1995             0           3
17        2011      2010    2001    2001    1995             0           3
18        2011      2011    2001    2001    1995             0           3

相关内容

  • 没有找到相关文章

最新更新