使用Lubridate进行第二轮操作

huangapple go评论83阅读模式
英文:

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。取第一个元素

我该如何解决这个问题?

英文:

使用Lubridate进行第二轮操作

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 &lt;- structure(list(measurement_date = c(&quot;2022-02-18 09:14:59&quot;, &quot;2022-02-19 08:29:59&quot;, 
&quot;2022-02-20 16:40:00&quot;, &quot;2022-02-21 09:00:00&quot;, &quot;2022-02-22 09:04:59&quot;, 
&quot;2022-02-23 09:59:59&quot;, &quot;2022-02-24 10:15:00&quot;, &quot;2022-02-25 08:35:59&quot;, 
&quot;2022-02-26 09:30:00&quot;, &quot;2022-02-27 15:00:00&quot;, &quot;2022-02-28 08:32:59&quot;, 
&quot;2022-03-01 09:35:00&quot;, &quot;2022-01-19 08:26:59&quot;)), class = &quot;data.frame&quot;, row.names = c(NA, 
-13L))

library(tidyverse)

mutate(df, measurement_date = round_date(as_datetime(measurement_date), unit = &quot;minute&quot;))

# 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 &lt;- structure(list(measurement_date = c(
&quot;2022-02-18 09:14:59&quot;, &quot;2022-02-19 08:29:59&quot;,
&quot;2022-02-20 16:40:00&quot;, &quot;2022-02-21 09:00:00&quot;, &quot;2022-02-22 09:04:59&quot;,
&quot;2022-02-23 09:59:59&quot;, &quot;2022-02-24 10:15:00&quot;, &quot;2022-02-25 08:35:59&quot;,
&quot;2022-02-26 09:30:00&quot;, &quot;2022-02-27 15:00:00&quot;, &quot;2022-02-28 08:32:59&quot;,
&quot;2022-03-01 09:35:00&quot;, &quot;2022-01-19 08:26:59&quot;
)), class = &quot;data.frame&quot;, row.names = c(
NA,
-13L
))
library(tidyverse)
df |&gt;
mutate(measurement_date_rounded = ceiling_date(
x = ymd_hms(measurement_date),
unit = &quot;minute&quot;
))
#&gt;       measurement_date measurement_date_rounded
#&gt; 1  2022-02-18 09:14:59      2022-02-18 09:15:00
#&gt; 2  2022-02-19 08:29:59      2022-02-19 08:30:00
#&gt; 3  2022-02-20 16:40:00      2022-02-20 16:40:00
#&gt; 4  2022-02-21 09:00:00      2022-02-21 09:00:00
#&gt; 5  2022-02-22 09:04:59      2022-02-22 09:05:00
#&gt; 6  2022-02-23 09:59:59      2022-02-23 10:00:00
#&gt; 7  2022-02-24 10:15:00      2022-02-24 10:15:00
#&gt; 8  2022-02-25 08:35:59      2022-02-25 08:36:00
#&gt; 9  2022-02-26 09:30:00      2022-02-26 09:30:00
#&gt; 10 2022-02-27 15:00:00      2022-02-27 15:00:00
#&gt; 11 2022-02-28 08:32:59      2022-02-28 08:33:00
#&gt; 12 2022-03-01 09:35:00      2022-03-01 09:35:00
#&gt; 13 2022-01-19 08:26:59      2022-01-19 08:27:00

huangapple
  • 本文由 发表于 2023年7月23日 23:44:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76749106.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定