英文:
Round Second with Lubridate
问题
我正在使用R,需要将时间从09:14:59四舍五入到09:15:00,我有多条记录如下。
我尝试使用lubridate包中的round_date
函数:
temperature = worksheet_temperature %>%
mutate(measurement_date = round_date(measurement_date, unit=second(as_datetime(as_date(measurement_date, origin = "1899-12-30")))))
但是我收到以下错误:
Error: `mutate()`列`measurement_date`出现问题。
i `measurement_date = round_date(...)`。
x 无效的周期名称:4
运行 `rlang::last_error()` 以查看错误发生的位置。
另外:警告信息:
`mutate()`列`measurement_date`出现问题。
i `measurement_date = round_date(...)`。
i 单位参数长度大于1。取第一个元素
我该如何解决这个问题?
英文:
I'm using R, I need to round the seconds referring to the hours 09:14:59 to 09:15:00 I have several records like this.
I tried to use the round_date from the lubridate package
temperature = worksheet_temperature %>%
mutate(measurement_date = round_date(measurement_date, unit=second(as_datetime(as_date(measurement_date, origin = "1899-12-30")))))
but I get the following error:
Error: Problem with `mutate()` column `measurement_date`.
i `measurement_date = round_date(...)`.
x Invalid period name: 4
Run `rlang::last_error()` to see where the error occurred.
In addition: Warning message:
Problem with `mutate()` column `measurement_date`.
i `measurement_date = round_date(...)`.
i Unit argument longer than 1. Taking first element
how can i solve this?
答案1
得分: 2
根据您对输出应该是什么样子的解释,您希望将时间舍入到最近的分钟。在round_date()
函数的unit
参数中,只需指定"minute"
:
lubridate::round_date(
lubridate::ymd_hms("2022-02-18 09:14:59"),
unit = "minute"
)
这将返回:"2022-02-18 09:15:00 UTC"
。
英文:
From your explanation of what the output should look like, you want to round to the nearest minute. In the unit
argument of round_date()
simply specify "minute"
:
lubridate::round_date(
lubridate::ymd_hms("2022-02-18 09:14:59"),
unit = "minute"
)
this returns: "2022-02-18 09:15:00 UTC"
答案2
得分: 1
根据您的描述,“将秒数四舍五入到... 09:14:59 到 09:15:00”,我假设您想要将时间四舍五入到最接近的分钟。以下是如何做到这一点的代码示例:
df <- structure(list(measurement_date = c("2022-02-18 09:14:59", "2022-02-19 08:29:59",
"2022-02-20 16:40:00", "2022-02-21 09:00:00", "2022-02-22 09:04:59",
"2022-02-23 09:59:59", "2022-02-24 10:15:00", "2022-02-25 08:35:59",
"2022-02-26 09:30:00", "2022-02-27 15:00:00", "2022-02-28 08:32:59",
"2022-03-01 09:35:00", "2022-01-19 08:26:59")), class = "data.frame", row.names = c(NA,
-13L))
library(tidyverse)
mutate(df, measurement_date = round_date(as_datetime(measurement_date), unit = "minute"))
# 输出:
measurement_date
1 2022-02-18 09:15:00
2 2022-02-19 08:30:00
3 2022-02-20 16:40:00
4 2022-02-21 09:00:00
5 2022-02-22 09:05:00
6 2022-02-23 10:00:00
7 2022-02-24 10:15:00
8 2022-02-25 08:36:00
9 2022-02-26 09:30:00
10 2022-02-27 15:00:00
11 2022-02-28 08:33:00
12 2022-03-01 09:35:00
13 2022-01-19 08:27:00
还有许多其他四舍五入日期时间的方法。请查看此处的round_date
文档。
英文:
Based on your description "round the seconds... 09:14:59 to 09:15:00" I assume you are wanting to round to the nearest minute. This is how you can do this:
df <- structure(list(measurement_date = c("2022-02-18 09:14:59", "2022-02-19 08:29:59",
"2022-02-20 16:40:00", "2022-02-21 09:00:00", "2022-02-22 09:04:59",
"2022-02-23 09:59:59", "2022-02-24 10:15:00", "2022-02-25 08:35:59",
"2022-02-26 09:30:00", "2022-02-27 15:00:00", "2022-02-28 08:32:59",
"2022-03-01 09:35:00", "2022-01-19 08:26:59")), class = "data.frame", row.names = c(NA,
-13L))
library(tidyverse)
mutate(df, measurement_date = round_date(as_datetime(measurement_date), unit = "minute"))
# Output:
measurement_date
1 2022-02-18 09:15:00
2 2022-02-19 08:30:00
3 2022-02-20 16:40:00
4 2022-02-21 09:00:00
5 2022-02-22 09:05:00
6 2022-02-23 10:00:00
7 2022-02-24 10:15:00
8 2022-02-25 08:36:00
9 2022-02-26 09:30:00
10 2022-02-27 15:00:00
11 2022-02-28 08:33:00
12 2022-03-01 09:35:00
13 2022-01-19 08:27:00
There are a variety of other ways of rounding datetimes though. See the documentation for round_date
here.
答案3
得分: 0
如果您想将所有日期都舍入到最接近的边界,您可以使用 ceiling_date()
。请参阅 r4ds。
df <- structure(list(measurement_date = c(
"2022-02-18 09:14:59", "2022-02-19 08:29:59",
"2022-02-20 16:40:00", "2022-02-21 09:00:00", "2022-02-22 09:04:59",
"2022-02-23 09:59:59", "2022-02-24 10:15:00", "2022-02-25 08:35:59",
"2022-02-26 09:30:00", "2022-02-27 15:00:00", "2022-02-28 08:32:59",
"2022-03-01 09:35:00", "2022-01-19 08:26:59"
)), class = "data.frame", row.names = c(
NA,
-13L
))
library(tidyverse)
df %>%
mutate(measurement_date_rounded = ceiling_date(
x = ymd_hms(measurement_date),
unit = "minute"
))
#> measurement_date measurement_date_rounded
#> 1 2022-02-18 09:14:59 2022-02-18 09:15:00
#> 2 2022-02-19 08:29:59 2022-02-19 08:30:00
#> 3 2022-02-20 16:40:00 2022-02-20 16:40:00
#> 4 2022-02-21 09:00:00 2022-02-21 09:00:00
#> 5 2022-02-22 09:04:59 2022-02-22 09:05:00
#> 6 2022-02-23 09:59:59 2022-02-23 10:00:00
#> 7 2022-02-24 10:15:00 2022-02-24 10:15:00
#> 8 2022-02-25 08:35:59 2022-02-25 08:36:00
#> 9 2022-02-26 09:30:00 2022-02-26 09:30:00
#> 10 2022-02-27 15:00:00 2022-02-27 15:00:00
#> 11 2022-02-28 08:32:59 2022-02-28 08:33:00
#> 12 2022-03-01 09:35:00 2022-03-01 09:35:00
#> 13 2022-01-19 08:26:59 2022-01-19 08:27:00
英文:
If you want to have all the dates rounded up to the nearest boundary, you can use ceiling_date()
. See r4ds.
df <- structure(list(measurement_date = c(
"2022-02-18 09:14:59", "2022-02-19 08:29:59",
"2022-02-20 16:40:00", "2022-02-21 09:00:00", "2022-02-22 09:04:59",
"2022-02-23 09:59:59", "2022-02-24 10:15:00", "2022-02-25 08:35:59",
"2022-02-26 09:30:00", "2022-02-27 15:00:00", "2022-02-28 08:32:59",
"2022-03-01 09:35:00", "2022-01-19 08:26:59"
)), class = "data.frame", row.names = c(
NA,
-13L
))
library(tidyverse)
df |>
mutate(measurement_date_rounded = ceiling_date(
x = ymd_hms(measurement_date),
unit = "minute"
))
#> measurement_date measurement_date_rounded
#> 1 2022-02-18 09:14:59 2022-02-18 09:15:00
#> 2 2022-02-19 08:29:59 2022-02-19 08:30:00
#> 3 2022-02-20 16:40:00 2022-02-20 16:40:00
#> 4 2022-02-21 09:00:00 2022-02-21 09:00:00
#> 5 2022-02-22 09:04:59 2022-02-22 09:05:00
#> 6 2022-02-23 09:59:59 2022-02-23 10:00:00
#> 7 2022-02-24 10:15:00 2022-02-24 10:15:00
#> 8 2022-02-25 08:35:59 2022-02-25 08:36:00
#> 9 2022-02-26 09:30:00 2022-02-26 09:30:00
#> 10 2022-02-27 15:00:00 2022-02-27 15:00:00
#> 11 2022-02-28 08:32:59 2022-02-28 08:33:00
#> 12 2022-03-01 09:35:00 2022-03-01 09:35:00
#> 13 2022-01-19 08:26:59 2022-01-19 08:27:00
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论