3

I have the following dataframe:

structure(list(mesos = c("abr22", "ag22", "des22"), Anual_2022 = c(66.8, 
66.8, 66.8), decil1 = c(100.1, 100.1, 100.1)), row.names = c(NA, 
-3L), class = c("tbl_df", "tbl", "data.frame"))

and I would like to convert mesos to date format.

So far, I have tried:

taxa_inflacio_ESP_mesos2022$mesos <- lubridate::parse_date_time(taxa_inflacio_ESP_mesos2022$mesos , orders = c("%m%y", "%m/%Y"))
taxa_inflacio_ESP_mesos2022$mesos<- ymd(taxa_inflacio_ESP_mesos2022$mesos) 

But not all the values of mesos are converted to date format.

zx8754
  • 52,746
  • 12
  • 114
  • 209
Maria
  • 173
  • 7
  • 1
    Why is it `ag22` and not `ago22` ? Is it not 3 letter month names? – zx8754 Mar 30 '23 at 08:35
  • It's in catalan... but I can change it in my excel file. – Maria Mar 30 '23 at 08:59
  • With abbreviated month names (`%b`), you may need to set your locale; see [strptime, as.POSIXct and as.Date return unexpected NA](https://stackoverflow.com/questions/13726894/strptime-as-posixct-and-as-date-return-unexpected-na) – Henrik Mar 30 '23 at 09:40
  • [Getting an NA with abbreviated month format for as.Date in Spanish locale](https://stackoverflow.com/questions/66304832/getting-an-na-with-abbreviated-month-format-for-as-date-in-spanish-locale) – Henrik Mar 30 '23 at 09:46
  • If you set the locale to "Catalan - SPAIN (ES)", `Sys.setlocale("LC_TIME", "ca_ES")`, you may check the standard abbreviated month names of this locale: `format(as.Date(paste0("2022-", 1:12, "-01")), "%b")`. To parse your month names correctly, they need to have this format. – Henrik Mar 30 '23 at 10:08
  • 1
    @zx8754 If you run the code in my previous comment, you find that the abbreviated month names in catalan locale is not very consistent; they may have either 2, 3, and 4 letters (followed by a period, in most cases...), e.g. `ag.` and `febr.`. There's also `jul.` and `juny`. – Henrik Mar 30 '23 at 10:20
  • I'm a bit confused... So I did what @Henrik said: `Sys.setlocale("LC_TIME", "ca_ES")` `Sys.getlocale("LC_TIME")` and `format(as.Date(paste0("2022-", 1:12, "-01")), "%b")`. The months in my dataset appears as they are shown with this code, but without the dot. – Maria Mar 30 '23 at 10:27
  • @Maria If you study the [question](https://stackoverflow.com/questions/66304832/getting-an-na-with-abbreviated-month-format-for-as-date-in-spanish-locale) I posted above, you find that OP there describes the need for a period `.` to parse the months correctly. See also my next comment about the need for period _in most cases_. – Henrik Mar 30 '23 at 10:32
  • @Henrik thanks for info, I assumed all abbr month names are 3 letters, and thought OP had a typo in their example. – zx8754 Mar 30 '23 at 11:55
  • @zx8754 Indeed, it seems quite tricky to remember the format(s) in that locale... – Henrik Mar 30 '23 at 12:18

0 Answers0