Search code examples
rstrptime

strptime() returns NA for char string - Locale language issue, Brazil Portuguese


First of all, I'm below starter level and am just starting to learn coding.

I'm tring to convert a character string into time using strptime() as follows:

t3 <- "October 17, 1986 08:24"
t4 <- strptime(t3, "%B %d, %Y %H:%M")

t4

class(t4)

This is what it prints:

t3 <- "October 17, 1986 08:24"

t4 <- strptime(t3, "%B %d, %Y %H:%M")

t4
[1] NA

class(t4)
[1] "POSIXlt" "POSIXt"

I'm using this on a scripted class - swirl() - and even though t4 returns NA, it accepted as a correct answer to the task.

Tried using R Studio, R 3.3.1 and running R on a command line, as shown on the picture. (stack won't let me post it since my reputation is still low)

I also tried assigning "outubro" and "Outubro" to t3 since my locale is Brazil.


Solution

  • @thelatemail recommended me to check local month names via format(ISOdate(2000, 1:12, 1), "%B")

    My locale and language were the issue. In Portuguese, months should be used without capital letters.

    Now:

    t3 <- "outubro 17, 1986 08:24" t4 <- strptime(t3, "%B %d, %Y %H:%M") [1] "1986-10-17 08:24:00 BRT"