dplyr's filter not working on lubridate's timeformats?

Viewed 138

When trying to answering this question, I run against a problem with using filter from dplyr-package on a lubridat-period column.

Example data:

df <- data.frame(time = ms(c('0:19','1:24','7:53','11:6')), value = 1:4)

Using:

filter(df, time > ms('5:00'))
# or:
filter(df, time > '5M 00S')

results in the wrong output:

   time value
1   53S     3
2 1M 6S     4
Warning message:
In format.data.frame(x, digits = digits, na.encode = FALSE) :
  corrupt data frame: columns will be truncated or padded with NAs

Applying the solution from this answer doesn't also result in the correct output:

> df %>% 
+   mutate(time = format(time, '%M:%S')) %>% 
+   filter(time > '05:00')
    time value
1    19S     1
2 1M 24S     2
3 7M 53S     3
4 11M 6S     4

But using vanilla R methods, do work:

> df[df$time > ms('5:00'), ]
    time value
3 7M 53S     3
4 11M 6S     4

> subset(df, time > ms('5:00'))
    time value
3 7M 53S     3
4 11M 6S     4

Is there anything I'm doing wrong in my dplyr approach?

1 Answers

After trying a lot of different methods I get a dplyr only solution:

df %>% 
  mutate(time = as.numeric(time)) %>% 
  filter(time > as.numeric(ms('5:00'))) %>% 
  mutate(time = ms(paste0(floor(time/60),':',round((time/60 - floor(time/60))*60))))

This results in the good result:

    time value
1 7M 53S     3
2 11M 6S     4
Related