I'm using R Studio. When I try to convert the date and time format using as.Date or as.Time I'm getting NA as the result. I also tried to set the Locale as it has been recommended in some of the problems in SO, that's also not helping. The default class is factor after I import from the text file. I also tried to make it a character. Still the problem exists. Any help?
> x<-c("16-12-2006")
> class(x)
[1] "character"
> y<-as.Date(x)
> class(y)
[1] "Date"
> y<-as.Date(x,format="d%m%Y%")
> class(y)
[1] "Date"
> y
[1] NA
You are just misplacing the %
s and missing the -
s in your format string. The format string needs to match the string characters exactly (spaces, hyphens, commas, colons, etc.). See the document: Date-time Conversion Functions to and from Character .
Try:
> y <- as.Date(x, format="%d-%m-%Y")
and it should work.
Try this:
> x <- as.POSIXct(strptime("11-09-2015", "%d-%m-%Y"))
> as.Date(x)
[1] "2015-09-10"
> x
[1] "2015-09-11 CEST"
x is a class of "POSIXct" and "POSIXt", but as.Date(x) is a class of "Date" and you can use it as x-axis in ggplot.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.