提取日期的奇怪格式(欧元/mm/yyyy)

发布于 2025-02-12 17:23:22 字数 387 浏览 2 评论 0原文

我想从R的格式转换为Mmyyy的格式。 怪兽柱具有特征。

structure(list(Monat = c("EURO MTH/07/2015", "EURO MTH/08/2015", 
"EURO MTH/09/2015", "EURO MTH/10/2015", "EURO MTH/11/2015", "EURO MTH/12/2015"
), Gesamtpreis = c(145667346.4375, 138659005.976562, 152031299.125, 
127354805.953125, 141803880.734375, 146695055.171875)), row.names = c(NA, 
6L), class = "data.frame")

谢谢。

I want to convert the date in the format MMYYYY from a column which is in this format EURO MTH/MM/YYYY in R.
The Monat column is in character.

structure(list(Monat = c("EURO MTH/07/2015", "EURO MTH/08/2015", 
"EURO MTH/09/2015", "EURO MTH/10/2015", "EURO MTH/11/2015", "EURO MTH/12/2015"
), Gesamtpreis = c(145667346.4375, 138659005.976562, 152031299.125, 
127354805.953125, 141803880.734375, 146695055.171875)), row.names = c(NA, 
6L), class = "data.frame")

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞美少年 2025-02-19 17:23:22

这是一种方法,但没有包裹lubridate,基本r就足够了。

df1 <-
  structure(list(
    Monat = c("EURO MTH/07/2015", "EURO MTH/08/2015", 
              "EURO MTH/09/2015", "EURO MTH/10/2015", 
              "EURO MTH/11/2015", "EURO MTH/12/2015"), 
    Gesamtpreis = c(145667346.4375, 138659005.976562, 
                    152031299.125, 127354805.953125, 
                    141803880.734375, 146695055.171875)), 
    row.names = c(NA, 6L), class = "data.frame")

convertdate <- function(x) {
  tmp <- sub("^[^/]+/", "", x)
  tmp <- as.Date(paste0("1/", tmp), "%d/%m/%Y")
  format(tmp, "%m%Y")
}

convertdate(df1$Monat)
#> [1] "072015" "082015" "092015" "102015" "112015" "122015"

Here is a way but not with package lubridate, base R is enough.

df1 <-
  structure(list(
    Monat = c("EURO MTH/07/2015", "EURO MTH/08/2015", 
              "EURO MTH/09/2015", "EURO MTH/10/2015", 
              "EURO MTH/11/2015", "EURO MTH/12/2015"), 
    Gesamtpreis = c(145667346.4375, 138659005.976562, 
                    152031299.125, 127354805.953125, 
                    141803880.734375, 146695055.171875)), 
    row.names = c(NA, 6L), class = "data.frame")

convertdate <- function(x) {
  tmp <- sub("^[^/]+/", "", x)
  tmp <- as.Date(paste0("1/", tmp), "%d/%m/%Y")
  format(tmp, "%m%Y")
}

convertdate(df1$Monat)
#> [1] "072015" "082015" "092015" "102015" "112015" "122015"

Created on 2022-07-03 by the reprex package (v2.0.1)

執念 2025-02-19 17:23:22

这是lubridategsubstrftime的方法。

library(lubridate)
df <-
    structure(list(
        Monat = c(
            "EURO MTH/07/2015",
            "EURO MTH/08/2015",
            "EURO MTH/09/2015",
            "EURO MTH/10/2015",
            "EURO MTH/11/2015",
            "EURO MTH/12/2015"
        ),
        Gesamtpreis = c(
            145667346.4375,
            138659005.976562,
            152031299.125,
            127354805.953125,
            141803880.734375,
            146695055.171875
        )
    ),
    row.names = c(NA,
                                6L),
    class = "data.frame")
    
strftime(my(gsub('EURO MTH/','', df$Monat)), '%m%Y')

[1] "072015" "082015" "092015" "102015" "112015" "122015"

Here's a way with lubridate, gsub and strftime.

library(lubridate)
df <-
    structure(list(
        Monat = c(
            "EURO MTH/07/2015",
            "EURO MTH/08/2015",
            "EURO MTH/09/2015",
            "EURO MTH/10/2015",
            "EURO MTH/11/2015",
            "EURO MTH/12/2015"
        ),
        Gesamtpreis = c(
            145667346.4375,
            138659005.976562,
            152031299.125,
            127354805.953125,
            141803880.734375,
            146695055.171875
        )
    ),
    row.names = c(NA,
                                6L),
    class = "data.frame")
    
strftime(my(gsub('EURO MTH/','', df$Monat)), '%m%Y')

[1] "072015" "082015" "092015" "102015" "112015" "122015"
稚气少女 2025-02-19 17:23:22

这是使用Stringr软件包和REGEX的另一个选项:

library(dplyr)
library(stringr)

df1 %>% 
  mutate(Monat = str_extract(str_remove_all(Monat, '/'), '\\d{6}')) %>% 
  pull(Monat)

[1] "072015" "082015" "092015" "102015" "112015" "122015"

Here is another option using stringr package and regex:

library(dplyr)
library(stringr)

df1 %>% 
  mutate(Monat = str_extract(str_remove_all(Monat, '/'), '\\d{6}')) %>% 
  pull(Monat)

[1] "072015" "082015" "092015" "102015" "112015" "122015"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文