Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Formatting dates when converting to list

I have a table like this:

ID Date Status
101 2020-09-14 1
102 2020-09-14 1
103 2020-09-14 1
104 2020-09-14 2
105 2020-09-14 2
106 2020-09-14 2

But want a table like this:

Status ID Date
1 101,102,103 2020-09-14, 2020-09-14, 2020-09-14
1 104,105,106 2020-09-14, 2020-09-14, 2020-09-14

Code that i’m currently using:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

note: date is in format yyyy-mm-dd before running code.

g1 <- df1 %>% 
  mutate(Date = as.Date(Date, format = '%Y%m%d')) %>%
  group_by(status) %>% 
  summarise_at(c("ID", "Date"), list)

This seems to work except for the date in the new table is not in yyyy-mm-dd. For example, 2021-06-10 is converting to 18788.

>Solution :

A possible solution:

library(tidyverse)

df %>% 
  group_by(Status) %>% 
  summarise(ID = str_c(ID, collapse = ","), Date = str_c(Date, collapse = ","))

#> # A tibble: 2 × 3
#>   Status ID          Date                            
#>    <int> <chr>       <chr>                           
#> 1      1 101,102,103 2020-09-14,2020-09-14,2020-09-14
#> 2      2 104,105,106 2020-09-14,2020-09-14,2020-09-14

A more succinct alternative:

library(tidyverse)

df %>% 
  group_by(Status) %>% 
  summarise(across(c(ID, Date), str_c, collapse = ","))

#> # A tibble: 2 × 3
#>   Status ID          Date                            
#>    <int> <chr>       <chr>                           
#> 1      1 101,102,103 2020-09-14,2020-09-14,2020-09-14
#> 2      2 104,105,106 2020-09-14,2020-09-14,2020-09-14
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading