简体   繁体   English

将日期列中的NA替换为空白

[英]replace NA in date column with blank

I have a date column in my dataframe with some NA values. 我的数据框中有一个日期列,其中包含一些NA值。 I am trying to replace this NA values with blank using the command, df$FirstDate[is.na(df$FirstDate)] <- " " 我正在尝试使用命令df$FirstDate[is.na(df$FirstDate)] <- " "将此NA值替换为空白。
I am getting an error 我收到一个错误

Error in charToDate(x) : character string is not in a standard unambiguous format charToDate(x)中的错误:字符串不是标准的明确格式

Not sure how to fix this error. 不知道如何解决此错误。 Any help on this topic is much appreciated. 非常感谢您对此主题的任何帮助。

Here's the dput output from date column 这是日期列的dput输出

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

Your df$FirstDate column is of class Date . 您的df$FirstDate列属于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 . 这意味着您尝试在此列中分配的所有非NA值都将被强制转换为Date类,并且在强制转换过程中,您会看到此错误,因为字符串" "不是转换为Date类的标准格式。

If you are absolutely set on replacing NAs with spaces, convert df$FirstDate column to class character first like so: 如果您绝对想用空格替换NAsdf$FirstDatedf$FirstDate列转换为类character如下所示:

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

Now, go ahead and run: 现在,继续运行:

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

I had a similar cosmetic issue. 我有一个类似的化妆品问题。

Try converting the column to as.character.Date . 尝试将列转换为as.character.Date

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM