0

I am importing a csv, and it stores dates as month-year (Apr-02 for April 2002). In the csv file, there is a day in the value. So Apr-02 would actually have a value in the file as something like 4/10/2002. When I import the file into R, it keeps that Apr-02 format and stores it as a factor. I tried to use

ds$val <- as.Date(ds$val, format = "%b-%y")

to get it as a date, but it results in all NAs. I can do

ds$val <- strptime(paste("1", as.character(ds$val)), format="%d %b-%y")

but I lose the day. Is there a way to get the full date when importing the csv (aside from changing the format in the csv file)?

Thanks!

EDIT:first 5 rows in original format

Attached image of the csv data, in the first is the original format.

If I change the dates to normal format, values are: 4/10/2002, 8/24/1997, 1/6/1999, 10/10/2008, 8/16/1985

bnmsba14
  • 3
  • 3
  • 1
    If it is stored as `"Apr-02"` in text in a csv, I don't understand how it has a day. Is this an Excel formatting issue? – thelatemail Apr 05 '16 at 22:07
  • A date must have all of a day, month _and_ year. If all the values in your file have the same day, just different month/years, then you'll need to add that day before trying to convert it to a date. – joran Apr 05 '16 at 22:18
  • Agree with @thelatemail. Excel does a bunch of things when saving a file as csv. Open the csv in notepad++ and you will be surprised but it's not that excel didn't warn you before saving it as csv... – chinsoon12 Apr 06 '16 at 01:18
  • @thelatemail it is stored as a date, with a custom format "Apr-02". So there is actually a day in the value, but not in the displayed value. If I go into my csv file and change "Apr-02" to a normal date format, it will display as 4/10/2002 as an example. Changing the format for each csv is a possible solution, but I would like a programatic way of solving it. The dates are not all the same day. Adding a few rows of data to the original post – bnmsba14 Apr 06 '16 at 16:08
  • You don't have a csv then, you have an Excel file. A csv is plain text, with no formatting or fanciness whatsoever. I can only assume you have to change the formats in the Excel file to start with before importing. – thelatemail Apr 06 '16 at 22:37

1 Answers1

1

If you use stringsAsFactors=FALSE with the read.csv call you will not get factors but rather character values. But that's not the real problem, since as.Date will accept factors. You cannot use as.Date unless you paste a day-of-the-month value or use the zoo-pkg's yearmon-class.

IRTFM
  • 258,963
  • 21
  • 364
  • 487