简体   繁体   English

从 R 中的当前时间减去时间

[英]Subtracting time from current time in R

I am designing a flex dashboard for my office.我正在为我的办公室设计一个灵活的仪表板。 I have a column( lets say Entry time) containing time stamp(ex-2020-06-01 20:30).I want to remove those rows for which the diff between current time and entry time is greater than 24 hours.我有一个包含时间戳(ex-2020-06-01 20:30)的列(比如说进入时间)。我想删除当前时间和进入时间之间的差异大于 24 小时的那些行。 Can u please help?你能帮忙吗?

If you are tidyverse person you can do so using lubridate and filter pretty easily and then select to keep the columns you want after filtering.如果你是tidyverse人,你可以很容易地使用lubridate和 filter 然后select在过滤后保留你想要的列。

require(lubridate)
require(tidyverse)
df <- df%>%
     mutate(time_difference = interval(ymd_hm(start_column), ymd_hm(end_column))%>%
     filter(as.numeric(time_length(time_difference, 'hour')) >24)%>%
     select(-time_difference)

This takes a dataframe , creates a new column with a lubridate interval in it.这需要一个dataframe ,创建一个包含lubridate间隔的新列。 Then uses time length to get the duration in hours, which is coerced to numeric (just in case as some date objects are strings under the hood) within a filter to select times less than 24 hours.然后使用时间长度来获取以小时为单位的持续时间,这在过滤器中强制转换为数字(以防某些日期对象是引擎盖下的字符串)到 select 次小于 24 小时。 The last row using select simply removes the time_difference field created to do the filtering.最后一行使用select只是删除了为进行过滤而创建的time_difference字段。

This will all be saved back into the original dataframe.这将全部保存回原来的 dataframe。

Just check the grammar before you go.只需在 go 之前检查语法。 Without code to test it on, I may have missed a closing parenthesis or something somewhere~没有代码来测试它,我可能错过了一个右括号或某处的东西~

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM