在字符中拆分间隔日期并在 R 中转换为日期
splitting Interval Date in character and casting into Date in R
我在字符中有以下日期列(来自 ENTSO-E 平台)。我想转换为日期并提取此间隔的结束部分([1]“01.01.2021 00:00 - 01.01.2021 01:00”)。我收到错误“格式明确的字符串”。任何建议如何将字符格式的间隔的第二部分提取到 R 中的日期变量?
head(date)
- "01.01.2021 00:00 - 01.01.2021 01:00"
- "01.01.2021 01:00 - 01.01.2021 02:00"
- "01.01.2021 02:00 - 01.01.2021 03:00"
- "01.01.2021 03:00 - 01.01.2021 04:00"
- "01.01.2021 04:00 - 01.01.2021 05:00"
- "01.01.2021 05:00 - 01.01.2021 06:00"
Error in as.POSIXlt.character(x, tz, ...) :
character string is not in a standard unambiguous format
as.POSIXct(gsub("^.* - (.*)", "\1", x), format = "%d.%m.%Y %H:%M")
# [1] "2021-01-01 01:00:00 CET" "2021-01-01 02:00:00 CET" "2021-01-01 03:00:00 CET" "2021-01-01 04:00:00 CET"
# [5] "2021-01-01 05:00:00 CET"
示例数据
x <- c("01.01.2021 00:00 - 01.01.2021 01:00",
"01.01.2021 01:00 - 01.01.2021 02:00",
"01.01.2021 02:00 - 01.01.2021 03:00",
"01.01.2021 03:00 - 01.01.2021 04:00",
"01.01.2021 04:00 - 01.01.2021 05:00")
这里是 tidyverse
结合 lubridate
s dmy_hm
函数:
library(lubridate)
library(dplyr)
library(tidyr)
df %>%
separate(date, c("a", "date"), " - ") %>%
mutate(date = dmy_hm(date), .keep="used")
date
<dttm>
1 2021-01-01 01:00:00
2 2021-01-01 02:00:00
3 2021-01-01 03:00:00
4 2021-01-01 04:00:00
5 2021-01-01 05:00:00
6 2021-01-01 06:00:00
我在字符中有以下日期列(来自 ENTSO-E 平台)。我想转换为日期并提取此间隔的结束部分([1]“01.01.2021 00:00 - 01.01.2021 01:00”)。我收到错误“格式明确的字符串”。任何建议如何将字符格式的间隔的第二部分提取到 R 中的日期变量?
head(date)
- "01.01.2021 00:00 - 01.01.2021 01:00"
- "01.01.2021 01:00 - 01.01.2021 02:00"
- "01.01.2021 02:00 - 01.01.2021 03:00"
- "01.01.2021 03:00 - 01.01.2021 04:00"
- "01.01.2021 04:00 - 01.01.2021 05:00"
- "01.01.2021 05:00 - 01.01.2021 06:00"
Error in as.POSIXlt.character(x, tz, ...) : character string is not in a standard unambiguous format
as.POSIXct(gsub("^.* - (.*)", "\1", x), format = "%d.%m.%Y %H:%M")
# [1] "2021-01-01 01:00:00 CET" "2021-01-01 02:00:00 CET" "2021-01-01 03:00:00 CET" "2021-01-01 04:00:00 CET"
# [5] "2021-01-01 05:00:00 CET"
示例数据
x <- c("01.01.2021 00:00 - 01.01.2021 01:00",
"01.01.2021 01:00 - 01.01.2021 02:00",
"01.01.2021 02:00 - 01.01.2021 03:00",
"01.01.2021 03:00 - 01.01.2021 04:00",
"01.01.2021 04:00 - 01.01.2021 05:00")
这里是 tidyverse
结合 lubridate
s dmy_hm
函数:
library(lubridate)
library(dplyr)
library(tidyr)
df %>%
separate(date, c("a", "date"), " - ") %>%
mutate(date = dmy_hm(date), .keep="used")
date
<dttm>
1 2021-01-01 01:00:00
2 2021-01-01 02:00:00
3 2021-01-01 03:00:00
4 2021-01-01 04:00:00
5 2021-01-01 05:00:00
6 2021-01-01 06:00:00