• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Creating new dataset with only the most recent date in R

Resolved: Creating new dataset with only the most recent date in R

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I am trying to create a new table where table1 is grouped by ID and only the most recent date is picked from table1$new.
I have the tables below:
1st Table:
ID Status Date
001 1003 2021-02-01
001 1003 2021-02-01
001 1003 2021-02-21
001 1003 2021-04-21
002 1003 2021-05-01
002 1003 2021-05-01
002 1003 2021-05-21
002 1003 2021-07-21
003 1004 2021-05-01
003 1004 2021-05-01
003 1004 2021-05-21
003 1004 2021-07-21

Current Output
ID Status Date
001 1003 2021-02-01
001 1003 2021-02-01
001 1003 2021-02-21
001 1003 2021-04-21
002 1003 2021-05-01
002 1003 2021-05-01
002 1003 2021-05-21
002 1003 2021-07-21

Desired Output:
ID New
001 2021-4-21
002 2021-07-21

Here is my code as of now
desired <- table1 %>% 
group_by(ID) %>% 
subset(Status == '1003', select = c("ID", "Date")) %>% rename(New = Date)
I think my first mistake is in the group_by and I am not sure how to only keep the most recent date. Any help would be appreciated.

Answer:

I would use dplyr::slice_max() to select the most recent date.
library(tidyverse)
d <- tibble::tribble(
  ~ID, ~Status,        ~Date,
  "001",   1003L, "2021-02-01",
  "001",   1003L, "2021-02-01",
  "001",   1003L, "2021-02-21",
  "001",   1003L, "2021-04-21",
  "002",   1003L, "2021-05-01",
  "002",   1003L, "2021-05-01",
  "002",   1003L, "2021-05-21",
  "002",   1003L, "2021-07-21",
  "003",   1004L, "2021-05-01",
  "003",   1004L, "2021-05-01",
  "003",   1004L, "2021-05-21",
  "003",   1004L, "2021-07-21"
)

res <- d |> 
  mutate(Date = lubridate::ymd(Date)) |> 
  dplyr::filter(Status == 1003) |> 
  group_by(ID) |> 
  slice_max(Date, n = 1) |> 
  ungroup() |> 
  select(ID, New = Date) # rename variable in the selection of variables
res
#> # A tibble: 2 x 2
#>   ID    New       
#>   <chr> <date>    
#> 1 001   2021-04-21
#> 2 002   2021-07-21
Created on 2022-06-15 by the reprex package (v2.0.1)

If you have better answer, please add a comment about this, thank you!

r
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Passing 2 functions in onChange in react

24/03/2023

Resolved: Why doesn’t stringstream consume output during hex formatting?

24/03/2023

Resolved: CMake path for python3 libraries doesn’t change (windows 10)

24/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.