Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extract date which is in strange format (EURO MTH/MM/YYYY) in R

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading