Home > Mobile >  I get NA using as.Date in r
I get NA using as.Date in r

Time:12-05

When I use the "as.Date" command in r it returns Na as results, I have tried different things like transforming it to POSIXt format, but it doesn't work

#this is the format i need it to read
format(Sys.Date(), "%B de %Y")

# this is an example of what i need
date="noviembre de 2022"
class(date)

#this is what i tried
date2 <- strptime(date, format = "%B de %Y")
class(date2)
date3 <- as.Date(date2,format="%B de %Y")
class(date3)
date3

This is what i get na as result

I would like to have something similar to this

navidad2021=as.Date("25 Diciembre 2021",format="%d %B %Y")
navidad2021
[1] "2021-12-25"

But in this format

format(Sys.Date(), "%B de %Y")
[1] "December de 2022"

Thank you very much for the help

CodePudding user response:

It looks like the issue you are experiencing is with the date format string that you are using with the strptime and as.Date functions. In your example, you are using the format string "%B de %Y", which specifies that the month is in full text form (e.g. "noviembre") and is followed by the string "de" and the year. However, this format string does not match the format of the date string that you are trying to parse, which is in the form "noviembre de 2022".

To fix this issue, you will need to use a format string that matches the format of the date string. In this case, you can use the following format string: "%B de %Y". Here is an example of how this can be done:

# Define the date string
date <- "noviembre de 2022"

# Parse the date string using the correct format string
date2 <- strptime(date, format = "%B de %Y")

# Convert the date to a Date object
date3 <- as.Date(date2,format="%B de %Y")

# Print the date
date3

This should return the date in the correct format, with the month in full text form and the year. You can then use the format function to convert the date to the desired format, as shown in your example.

CodePudding user response:

EDIT- I may've misunderstood your intention. If you want to create a date from the phrase "November de 2022" (my computer's local settings use english), we could do this by manipulating the string to add "1 " at the start and to remove "de " from the middle -- then it parses.

date="November de 2022"
as.Date(paste(1, gsub("de ", "", date)), format = "%d %B %Y")
[1] "2022-11-01"

Date is one of the built-in data types in R. When you print a Date, it uses the built-in print.Date function to show it as YYYY-MM-DD. You can also create a character string (as you have done) to display a Date using another format, but then it will cease to be a Date you can manipulate.

You could overwrite print.Date on your system if you want it to print differently.

print.Date <- function (x, max = NULL, ...) 
{
  if (is.null(max)) 
    max <- getOption("max.print", 9999L)
  if (max < length(x)) {
    print(format(x[seq_len(max)]), max = max   1, ...)
    cat(" [ reached 'max' / getOption(\"max.print\") -- omitted", 
        length(x) - max, "entries ]\n")
  }
  else if (length(x)) 
    print(format(x, "%B de %Y"), max = max, ...)
  else cat(class(x)[1L], "of length 0\n")
  invisible(x)
}

as.Date("2021-12-25")
[1] "December de 2021"
as.Date("2021-12-25")   7
[1] "January de 2022"

However, this will not work for others unless you overwrite their print.Date as well, which is bad form.

  • Related