replace NA in date column with blank
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)
Bridgeport Byron Tucker
Updated on July 25, 2022Comments
-
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 errorError 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 columndf = structure(c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), class = "Date")
-
jeremycg over 8 yearsWe can't help without seeing some of the data, try adding dput(df$FirstDate) to your question
-
Admin over 8 yearsDo you really have
NA
or is it<NA>
? -
Bridgeport Byron Tucker over 8 years@Pascal, i have NA in my date column
-
Admin over 8 years
as.Date()
knows how to manageNA
. See the output ofas.Date(NA)
. But this function expects a certain structure of character, to coerce this character into date." "
is a character, with no distinct structure andas.Date()
doesn't know how to deal with this, i.e.as.Date(" ")
gives the same error you mentioned. -
Bridgeport Byron Tucker over 8 years@Pascal, nice answer. I see my problem now.
-
-
Bridgeport Byron Tucker over 8 yearsthat 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 over 8 yearsCan you provide some motivation for why you want to replace
NAs
with this specific string" "
? -
aashanand over 8 yearsFrom 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 aDate
vector, you'll need to coerce it into classcharacter
. If you don't like howNAs
look, you'll either have to impute missing values or replace them with a standard missing value like1970-01-01
. -
Bridgeport Byron Tucker over 8 yearsi 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