replace NA in date column with blank

I have a date column in my dataframe with some NA values. I am trying to replace this NA values with blank using the command, df$FirstDate[is.na(df$FirstDate)] <- " "
I am getting an error

Error in charToDate(x) : character string is not in a standard unambiguous format

Not sure how to fix this error. Any help on this topic is much appreciated.

Here's the dput output from date column

df = structure(c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), class = "Date")

Upvotes: 2

Views: 22356

Answers (2)

Robbie Walsh
Robbie Walsh

Reputation: 11

I had a similar cosmetic issue.

Try converting the column to as.character.Date.

df$FirstDate <- as.character.Date(df$FirstDate)

Upvotes: 1

aashanand
aashanand

Reputation: 754

Your df$FirstDate column is of class Date.

This means that any non-NA values you try to assign in this column will be coerced into class Date, and in the process of coercion you are seeing this error because the string " " is not in a standard unambiguous format for conversion into class Date.

If you are absolutely set on replacing NAs with spaces, convert df$FirstDate column to class character first like so:

> df$FirstDate <- as.character(df$FirstDate)

Now, go ahead and run:

> df$FirstDate[is.na(df$FirstDate)] <- " "

Upvotes: 7

Related Questions