0

I'd like to convert a whole column with as.Date to a date format.

str(fluesse)
'data.frame':   1049 obs. of  28 variables:
$ Datum                      : Date, format: NA NA NA ...

the dates are formatted like this:

07.08.14

This is what I tried

as.Date(as.character("07.08.14"), format="%d.%m.%y")
[1] "2014-08-07"
as.Date(as.character(fluesse$Datum), format = "%d.%m.%y")
[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA

I always get NA for the entries in fluesse$Datum

and when I try to check what class it has i gives me

class("fluesse$Datum")
[1] "character"

What am I missing when I try to use as.character on the data.frame?

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Brkhn
  • 1
  • 1
  • 2
  • Can you show an example that reproduce NA (instead of the object name)? – akrun May 26 '15 at 08:54
  • from your example seems like your `Datum` column is filled with words `NA` not with dates in string format. maybe it's an issue with reading data to the `fluesse` data.frame, not with converting? – inscaven May 26 '15 at 09:06

2 Answers2

2

Well, your fluesse$Datum in the str(.) is already of class "Date" with all the NAs. Then below, you show another fluesse$Datum which is of class "character", so as.character(.) will not have an effect.

Maybe you just confused yourself by calling both versions fluesse instead of say d.fl.1 and d.fl.2 (one convention likes to denote datasets by an initial d.) ?

Martin Mächler
  • 4,619
  • 27
  • 27
1

For all date conversions... Trying the "lubridate" package always helps. Functions like dmy_hms, mdy_hms help in parsing almost any date format. Just install the package and play around with it. I think you will figure out yourself.You can try this:

> library(lubridate)
> x <- "07.08.14"
> x <- dmy(x)
> as.Date(x, format="%d.%m.%y")
[1] "2014-08-07"

in place of "x" you can put your date vector and try.

Another possibility is that, the data has not been read in from the source, hence class() on date vector gives character "NA".

NEO
  • 441
  • 1
  • 4
  • 12