bnmsba14
bnmsba14

Reputation: 3

R import csv lose date (day) value

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

Upvotes: 0

Views: 368

Answers (1)

IRTFM
IRTFM

Reputation: 263311

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.

Upvotes: 1

Related Questions