replace NA in date column with blank

19,609

Solution 1

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)] <- " "

Solution 2

I had a similar cosmetic issue.

Try converting the column to as.character.Date.

df$FirstDate <- as.character.Date(df$FirstDate)
Share:
19,609
Bridgeport Byron Tucker
Author by

Bridgeport Byron Tucker

Updated on July 25, 2022

Comments

  • Bridgeport Byron Tucker
    Bridgeport Byron Tucker almost 2 years

    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")
    
    • jeremycg
      jeremycg over 8 years
      We can't help without seeing some of the data, try adding dput(df$FirstDate) to your question
    • Admin
      Admin over 8 years
      Do you really have NA or is it <NA>?
    • Bridgeport Byron Tucker
      Bridgeport Byron Tucker over 8 years
      @Pascal, i have NA in my date column
    • Admin
      Admin over 8 years
      as.Date() knows how to manage NA. See the output of as.Date(NA). But this function expects a certain structure of character, to coerce this character into date. " " is a character, with no distinct structure and as.Date() doesn't know how to deal with this, i.e. as.Date(" ") gives the same error you mentioned.
    • Bridgeport Byron Tucker
      Bridgeport Byron Tucker over 8 years
      @Pascal, nice answer. I see my problem now.
  • Bridgeport Byron Tucker
    Bridgeport Byron Tucker over 8 years
    that I know, I am not too excited about converting date to char, I have to deal with format, origin and those issues while converting char to date back again later, I prefer something that works on the column with class date.
  • aashanand
    aashanand over 8 years
    Can you provide some motivation for why you want to replace NAs with this specific string " "?
  • aashanand
    aashanand over 8 years
    From the documentation for the class Date: "Dates are represented as the number of days since 1970-01-01, with negative values for earlier dates...It is intended that the date should be an integer..." If you want to insert a string into a Date vector, you'll need to coerce it into class character. If you don't like how NAs look, you'll either have to impute missing values or replace them with a standard missing value like 1970-01-01.
  • Bridgeport Byron Tucker
    Bridgeport Byron Tucker over 8 years
    i see the problem here, you and Pascal explained this perfectly. I am not very keen on replacing the NA with blanks now. My priority is to retain the class date, converting to char, then replacing NA is too much work. I fine as is