speendo
speendo

Reputation: 13345

convert factor to date with empty cells

I have a factor vector x looking like this:

""
"1992-02-13"
"2011-03-10"
""
"1998-11-30"

Can I convert this vector to a date vector (using as.Date())?

Trying the obvious way gives me:

> x <- as.Date(x)
Error in charToDate(x) :
character string is not in a standard unambiguous format

At the moment I solve this problem like this:

> levels(x)[1] <- NA
> x <- as.Date(x)

But this doesn't look too elegant...

Thank you in advance!

Upvotes: 1

Views: 6153

Answers (3)

Andrie
Andrie

Reputation: 179468

You simply need to tell as.Date what format to expect in your character vector:

xd <- as.Date(x, format="%Y-%m-%d")
xd
[1] NA           "1992-02-13" "2011-03-10" NA           "1998-11-30"

To illustrate that these are indeed dates:
xd[3] - xd[2]
Time difference of 6965 days

PS. This conversion using as.Date works regardless of whether your data is a character vector or a factor.

Upvotes: 8

Roman Luštrik
Roman Luštrik

Reputation: 70653

I usually convert factors to a POSIX* type class using the function strptime. First argument is your vector and the second argument is the "pattern" by which the date/time is constructed (a % sign + a specific letter). You basically tell R that first you have a year, then you have a -, then a month and so on. See ?strptime for a full list of conversion specifications.

x <- factor(c("1992-02-13", "2011-03-10", "1998-11-30"))
(x.date <- strptime(x, format = "%Y-%m-%d"))
 [1] "1992-02-13" "2011-03-10" "1998-11-30"
class(x.date)
 [1] "POSIXlt" "POSIXt" 

The same principle holds for as.Date. You tell R to "make this a date/time object and here are the instructions on how to make it".

(as.Date(x, "%Y-%m-%d"))
 [1] "1992-02-13" "2011-03-10" "1998-11-30"

Upvotes: 1

Brandon Bertelsen
Brandon Bertelsen

Reputation: 44658

When you pull in the data with read.csv, or others, you can set

read.csv(...,na.strings=c(""))

to avoid having to deal with this entirely.

Upvotes: 2

Related Questions