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

回眸只為那壹抹淺笑 提交于 2019-12-10 20:25:16

问题


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:


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


来源:https://stackoverflow.com/questions/46361342/dplyrs-filter-not-working-on-lubridates-timeformats

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!