简体   繁体   English

从 R 中的列中提取公共日期

[英]Extracting common date from a column in R

Let's say I have a column that has multiple repetitive dates, and I would like to extract the common dates, how can I do this in R using dplyr ?假设我有一column有多个重复日期,我想提取公共日期,我如何使用dplyrR执行此dplyr

Sample样本

1/1/2004
1/1/2004
1/1/2004 
1/2/2004
1/2/2004
2/3/2004
2/3/2004
3/4/2004
3/4/2004

Desired output期望输出

1/1/2004
1/2/2004
2/3/2004
3/4/2004

You can achieve this using summarise :您可以使用summarise实现此目的:

library(tidyverse)

dates <- tibble(
  dates = c("2004/01/01", 
            "2004/01/01", 
            "2004/01/01", 
            "2004/01/01",
            "2004/01/02",
            "2004/01/03",
            "2004/01/04")
)

dates %>% 
  mutate(dates = lubridate::ymd(dates)) %>% 
  group_by(dates) %>% 
  summarise(dates = max(dates))
#> # A tibble: 4 × 1
#>   dates     
#>   <date>    
#> 1 2004-01-01
#> 2 2004-01-02
#> 3 2004-01-03
#> 4 2004-01-04

Created on 2021-11-08 by the reprex package (v2.0.1)reprex 包(v2.0.1) 于 2021 年 11 月 8 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM