Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何选择id的第二个最后日期?_R_Dplyr - Fatal编程技术网

R 如何选择id的第二个最后日期?

R 如何选择id的第二个最后日期?,r,dplyr,R,Dplyr,我有一个数据帧 abc<- children_info child_id custody_start custody_end 1 01/01/2018 03/29/2018 1 04/15/2018 04/30/2018 1 05/01/2018 05/01/2018 2 05/26/2018 05/28/2018 2 05/29/2018 06/15/2

我有一个数据帧

abc<- children_info
child_id  custody_start   custody_end
1         01/01/2018      03/29/2018
1         04/15/2018      04/30/2018
1         05/01/2018      05/01/2018
2         05/26/2018      05/28/2018
2         05/29/2018      06/15/2018
2         06/16/2018      06/16/2018
3         03/22/2018      07/15/2019
3         01/09/2019      05/09/2019
3         06/09/2019      06/09/2019
4         03/15/2020      03/29/2020
我尝试了这段代码,但由于某些原因,它没有选择正确的日期

dplyr::mutate(if_else(`custody_start`==`custody_end`,lag(as.Date(custody_start)),custody_start)) %>%

非常感谢您的帮助

我们可以为每个
孩子id
选择最后一个
监护开始日期和最后一个
监护结束日期

library(dplyr)
df %>%
  group_by(child_id) %>%
  summarise(custody_start = nth(custody_start, max(n() - 1,1)), 
            custody_end = last(custody_end))


#  child_id custody_start custody_end
#     <int> <fct>         <fct>      
#1        1 04/15/2018    05/01/2018 
#2        2 05/29/2018    06/16/2018 
#3        3 01/09/2019    06/09/2019 
#4        4 03/15/2020    03/29/2020 
库(dplyr)
df%>%
分组依据(子id)%>%
总结(监护权开始=第n次(监护权开始,最大值(n()-1,1)),
保管结束=最后一次(保管结束))
#儿童身份监护开始监护结束
#                     
#1        1 04/15/2018    05/01/2018 
#2        2 05/29/2018    06/16/2018 
#3        3 01/09/2019    06/09/2019 
#4        4 03/15/2020    03/29/2020 
数据

df <- structure(list(child_id = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 
4L), custody_start = structure(c(1L, 5L, 6L, 7L, 8L, 10L, 4L, 
2L, 9L, 3L), .Label = c("01/01/2018", "01/09/2019", "03/15/2020", 
"03/22/2018", "04/15/2018", "05/01/2018", "05/26/2018", "05/29/2018", 
"06/09/2019", "06/16/2018"), class = "factor"), custody_end = structure(c(1L, 
3L, 4L, 6L, 8L, 9L, 10L, 5L, 7L, 2L), .Label = c("03/29/2018", 
"03/29/2020", "04/30/2018", "05/01/2018", "05/09/2019", "05/28/2018", 
"06/09/2019", "06/15/2018", "06/16/2018", "07/15/2019"), class = "factor")), 
class = "data.frame", row.names = c(NA, -10L))
df
df <- structure(list(child_id = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 
4L), custody_start = structure(c(1L, 5L, 6L, 7L, 8L, 10L, 4L, 
2L, 9L, 3L), .Label = c("01/01/2018", "01/09/2019", "03/15/2020", 
"03/22/2018", "04/15/2018", "05/01/2018", "05/26/2018", "05/29/2018", 
"06/09/2019", "06/16/2018"), class = "factor"), custody_end = structure(c(1L, 
3L, 4L, 6L, 8L, 9L, 10L, 5L, 7L, 2L), .Label = c("03/29/2018", 
"03/29/2020", "04/30/2018", "05/01/2018", "05/09/2019", "05/28/2018", 
"06/09/2019", "06/15/2018", "06/16/2018", "07/15/2019"), class = "factor")), 
class = "data.frame", row.names = c(NA, -10L))