Delete all times less than a specified value

2019-07-23 15:31发布

问题:

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
.
.
.

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!

回答1:

You can do that with:

df <- df[df$time > ms('5:00'), ]

The result:

> df
    time value
3 7M 53S     3
4 11M 6S     4

Strangly enough, converting this to dplyr code; it doesn't work:

filter(df, time > ms('5:00'))

The result:

   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))))

Data:

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


回答2:

Try This..

> library(lubridate)
> TimeData <- data.frame(Time = c("0M 19S", "1M 24S", "7M 53S", "11M 6S"))
> TimeData$Time <- ms(TimeData$Time)
> subset(TimeData, Time > "5M 00S")
    Time
3 7M 53S
4 11M 6S