0

I've got this dataframe from google.finance and I want the "Date" column to be in date format.

`read.csv("ual.csv", header = TRUE, stringsAsFactors = FALSE)?
 head(Ual_df)
       Date  Open  High   Low Close  Volume
1 19-May-17 76.42 78.34 76.18 77.98 4143616
2 18-May-17 76.32 77.18 75.62 75.98 3559591
3 17-May-17 78.06 78.63 75.94 76.18 5673018` 

As you can see, the dates are a bit ambiguous, since the days and the years are in the same format. I did this:

Ual_df$Date <- as.Date(Ual_df$Date, "%Y-%m-%d")

and it returned NAs.

  head(Ual_df)
  Date  Open  High   Low Close  Volume
1 <NA> 76.42 78.34 76.18 77.98 4143616
2 <NA> 76.32 77.18 75.62 75.98 3559591
3 <NA> 78.06 78.63 75.94 76.18 5673018

I also tried to set the C locale but nothing changed.

Any suggestions ? Thank you.

Sotos
  • 51,121
  • 6
  • 32
  • 66
27titanik
  • 21
  • 5

2 Answers2

0

We need %b and the order should be changed

Ual_df$Date <- as.Date(Ual_df$Date, "%d-%b-%y")
Ual_df$Date
#[1] "2017-05-19" "2017-05-18" "2017-05-17"
akrun
  • 874,273
  • 37
  • 540
  • 662
0

The lubridate package is best for dealing with dates. The following will even work if your Ual_df$Date column is a factor:

library(lubridate)
Ual_df$Date = dmy(Ual_df$Date)
lebelinoz
  • 4,890
  • 10
  • 33
  • 56