r - 如何过滤不同行中的日期?
问题描述
假设我有一个数据框
abc<- children_info
child_name custody_start custody_end
jon 01/01/2018 04/29/2018
jon 05/01/2018 05/25/2018
jon 05/29/2018 07/31/2018
paul 03/22/2018 07/15/2019
paul 06/09/2019 03/28/2020
我想过滤每个孩子的第一个监护开始日期和最后一个监护结束日期,并在新列中对其进行变异。在 dplyr 包中,最好的方法是什么?
解决方案
这个怎么样:
library(dplyr)
mydat %>%
mutate_at(vars(custody_start, custody_end), ~ as.Date(., "%m/%d/%Y")) %>%
group_by(child_name) %>%
summarize(
custody_start = min(custody_start),
custody_end = max(custody_end)
)
# # A tibble: 2 x 3
# child_name custody_start custody_end
# <chr> <date> <date>
# 1 jon 2018-01-01 2018-07-31
# 2 paul 2018-03-22 2020-03-28
数据:
mydat <- read.table(header=TRUE, stringsAsFactors=FALSE, text="
child_name custody_start custody_end
jon 01/01/2018 04/29/2018
jon 05/01/2018 05/25/2018
jon 05/29/2018 07/31/2018
paul 03/22/2018 07/15/2019
paul 06/09/2019 03/28/2020")
推荐阅读
- maven - 有没有办法找出为什么我的 Maven 项目不承认某些类?
- angular - Cypress:如何访问第三方组件?
- typescript - 在 knockout.js (TypeScript) 中更改 observable 但不通知订阅者
- c++ - 如何使用接口从 C++ 中的模板强制使用通用数据类型?
- flutter - 尝试在颤振中使用 MultipartRequest 上传文件时出现握手异常
- python - Pandas 组合多个特定的先前行
- c - 如何使用 Intel Advisor 分析我的并行 MPI 应用程序?
- docker - 将 VNC 与 Docker 一起使用
- multithreading - DbSet.Add(和其他非数据库操作)线程安全吗?
- reactjs - React 中父组件如何重置多个子组件