-3

I'm new to R and trying to load some time series data but I'm stuck at the first hurdle.

I have a dataframe with a date column called Date. The date format of the data is: 23-May-16 (it appears like this in the R console when I print df). To read as date I'm trying:

df$Date <- as.Date(df$Date, "%dd-%bbb-%yy")

as per guidance here

which produces the value <NA> when it reads the data.

joshi123
  • 835
  • 2
  • 13
  • 33
  • is "df$Date" a `character`? Try `str(df)` ? – DJJ May 18 '17 at 14:09
  • apparently `df$Date` is a factor... `ï..Date: Factor w/ 251 levels` – joshi123 May 18 '17 at 14:11
  • may be the first thing is to convet the factor to char first. [here](http://stackoverflow.com/questions/2851015/convert-data-frame-columns-from-factors-to-characters#2851213). You can do `options(stringsAsFactors=FALSE)` and reread your `data.frame` as well. Then @agerom solution seems fine. Cheers! – DJJ May 18 '17 at 14:14
  • 2
    You are just using the wrong format and you should really read the docs of `?as.Date` or `?strptime` which have similar examples. factors have nothing to do with this. – David Arenburg May 18 '17 at 14:18
  • 1
    `df$Date <- as.character.Date(df$Date)` or use `strptime` or the package `anytime` and list goes on and on. Or you can provide a reproducible example. – M-- May 18 '17 at 14:18

2 Answers2

2

Try:

as.Date(df$Date,format="%d-%b-%y")

amonk
  • 1,769
  • 2
  • 18
  • 27
  • glad it helps. As a separate note bear in mind that 'lubridate' package's magic. Give it a shot – amonk May 18 '17 at 15:03
0

You only need to list those once:

as.Date("23-May-16", "%d-%b-%y")
troh
  • 1,354
  • 10
  • 19