- Notifications
You must be signed in to change notification settings - Fork35
Description
It appears that roworder() does not sort columns of class Interval from lubridate. If roworder() is applied to a data frame that contains an Interval class column, all non-Interval columns will be sorted as specified, but the Interval class columns will remain in the order as in the originating data frame.
The same issue occurs with data.table's setorder(). A workaround is to use dplyr's arrange(), which correctly sorts all columns, regardless of class. Of course, arrange() can be much slower depending on the size of your data frame.
Another alternative is to convert the Interval class column(s) to character before using roworder(), but if the Interval class is needed for later operations, this might not be ideal.
Similar to another issue pertaining to lubridate Intervals (see#186), this may be more of a limitation than a bug. In the latter case, including a note in the documentation might be helpful and/or a warning if an Interval class column is detected in the data frame being sorted.
library(tidyverse)#> Warning: package 'tidyverse' was built under R version 4.0.5#> Warning: package 'ggplot2' was built under R version 4.0.5#> Warning: package 'tibble' was built under R version 4.0.5#> Warning: package 'tidyr' was built under R version 4.0.5#> Warning: package 'readr' was built under R version 4.0.5#> Warning: package 'purrr' was built under R version 4.0.5#> Warning: package 'dplyr' was built under R version 4.0.5#> Warning: package 'stringr' was built under R version 4.0.5#> Warning: package 'forcats' was built under R version 4.0.5#> Warning: package 'lubridate' was built under R version 4.0.5library(lubridate)library(collapse)#> Warning: package 'collapse' was built under R version 4.0.5#> collapse 1.9.3, see ?`collapse-package` or ?`collapse-documentation`#>#> Attaching package: 'collapse'#> The following object is masked from 'package:lubridate':#>#> is.Date#> The following object is masked from 'package:stats':#>#> DID<-c("E","B","A","C","D")Start<-c("3/1/2021","5/11/2018","6/12/2019","7/11/2018","3/3/2021")End<-c("3/3/2021","5/13/2018","6/15/2019","7/16/2018","3/5/2021")example<- tibble(ID,Start,End) %>% mutate(Start= mdy(Start),End= mdy(End),Interval= interval(Start,End))#Data frame, displayed as created, out of orderexample#> # A tibble: 5 × 4#> ID Start End Interval#> <chr> <date> <date> <Interval>#> 1 E 2021-03-01 2021-03-03 2021-03-01 UTC--2021-03-03 UTC#> 2 B 2018-05-11 2018-05-13 2018-05-11 UTC--2018-05-13 UTC#> 3 A 2019-06-12 2019-06-15 2019-06-12 UTC--2019-06-15 UTC#> 4 C 2018-07-11 2018-07-16 2018-07-11 UTC--2018-07-16 UTC#> 5 D 2021-03-03 2021-03-05 2021-03-03 UTC--2021-03-05 UTC#Data frame, ordered by ID, with "Interval" unaffected by sortexample %>% roworder(ID)#> # A tibble: 5 × 4#> ID Start End Interval#> <chr> <date> <date> <Interval>#> 1 A 2019-06-12 2019-06-15 2021-03-01 UTC--2021-03-04 UTC#> 2 B 2018-05-11 2018-05-13 2018-05-11 UTC--2018-05-13 UTC#> 3 C 2018-07-11 2018-07-16 2019-06-12 UTC--2019-06-17 UTC#> 4 D 2021-03-03 2021-03-05 2018-07-11 UTC--2018-07-13 UTC#> 5 E 2021-03-01 2021-03-03 2021-03-03 UTC--2021-03-05 UTC#Data frame, ordered by ID, with all columns correctly sortedexample %>% arrange(ID)#> # A tibble: 5 × 4#> ID Start End Interval#> <chr> <date> <date> <Interval>#> 1 A 2019-06-12 2019-06-15 2019-06-12 UTC--2019-06-15 UTC#> 2 B 2018-05-11 2018-05-13 2018-05-11 UTC--2018-05-13 UTC#> 3 C 2018-07-11 2018-07-16 2018-07-11 UTC--2018-07-16 UTC#> 4 D 2021-03-03 2021-03-05 2021-03-03 UTC--2021-03-05 UTC#> 5 E 2021-03-01 2021-03-03 2021-03-01 UTC--2021-03-03 UTC
Created on 2023-05-03 withreprex v2.0.2