删除小于指定值的所有时间 [英] Delete all times less than a specified value
问题描述
这可能是一个简单的问题,但我是R的新手,无法找到答案(或谷歌搜索错误的东西)。
我目前正在处理一个项目,该项目涉及删除少于5分钟的所有时间值。使用lubridate包创建时间的数据示例如下。
This is probably a simple question however I am new to R and couldn't find an answer (or was googling the wrong thing). I am currently working on a project which involves deleting all Time values that are less than 5 minutes. An example of the data is as follows with the times created using the "lubridate" package.
Time
19S
1M 24S
7M 53S
11M 6S
.
.
.
现在我希望删除所有小于5分钟的值。因此,我希望得到的最终数据集是:
Now I wish to delete all values which are less than 5 minutes. Therefore the final dataset I wish to get is:
Time
7M 53S
11M 6S
.
.
.
任何帮助都会令人惊叹!
谢谢!
Any help would be amazing! Thanks!
推荐答案
你可以这样做:
df <- df[df$time > ms('5:00'), ]
结果:
> df
time value
3 7M 53S 3
4 11M 6S 4
足够苛刻,将其转换为dplyr代码;它不起作用:
Strangly enough, converting this to dplyr code; it doesn't work:
filter(df, time > ms('5:00'))
结果:
time
1 53S
2 1M 6S
Warning message:
In format.data.frame(x, digits = digits, na.encode = FALSE) :
corrupt data frame: columns will be truncated or padded with NAs
我问了一个关于它的问题并找到了答案此处。你得到了很好的解决方案:
I asked a question about that and found a answer here. you get the good solution with:
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))))
数据:
df <- data.frame(time = ms(c('0:19','1:24','7:53','11:6')), value = 1:4)
这篇关于删除小于指定值的所有时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!