[R] Fwd: as.Date gives NAs when transforming from factor

Duncan Murdoch murdoch.duncan at gmail.com
Wed Apr 6 02:19:14 CEST 2016


On 05/04/2016 7:53 PM, Omar André Gonzáles Díaz wrote:
> Hi,
>
> I would appreciate your help.
>
> I’m having problems when transforming  a column from “factor” to “date”.
>
>
>
> It does not convert just: 31/03/2016 correctly, it out puts: NA.
>
>
>
> 04/04/2016  turns out as: 2016-04-04
>
>
>
> 02/04/2016 turns out as: 2016-02-04
>
>
>
> 31/03/2016 turns out as: NA
>
> 03/04/2016 turns out as: 2016-03-04.
>
>
>
>
>
>
>
> Code:
>
> a <- read.csv("dates.csv", stringsAsFactors = F)
>
>
>
> a$Date <- as.Date(a$Date, format = "%m/%d/%Y")


You are specifying month/day/year format. There's no month 31.
You probably want

a$Date <- as.Date(a$Date, format = "%d/%m/%Y")


Duncan Murdoch

>
>
>
>
> Posible Solutions:
>
>
>
> I’ve read here that it has to do with the Sys Locale, and the solution was
> using: “LC_TIME”, “C”. But I didn’t have success.
>
> http://stackoverflow.com/questions/15566875/as-date-returning-na-in-r
>
>
>
>
>
>
> # Sys.getlocale("LC_TIME")
>
> #
>
> # Sys.setlocale("LC_TIME", "C")
>
>
>
>
>
>
>
>
>
>
>
>
> Slds,
>
>
>
>
>
>
>
>
>
>
>
> ------------------------------------------------------------------------
>> Disclaimer The information in this email and any attachments may contain
> proprietary and privileged information that is intended for the
> addressee(s) only. If you are not the intended recipient, you are hereby
> notified that any disclosure, copying, distribution, retention or use of
> the contents of this information is prohibited. When addressed to our
> clients or vendors, any information contained in this e-mail or any
> attachments is subject to the terms and conditions in any governing
> contract. If you have received this e-mail in error, please immediately
> contact the sender and delete the e-mail..
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list