简体   繁体   中英

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 ?

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 :

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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