gpt4 book ai didi

r - 在 R 中将空值转换为 NULL - 处理日期列

转载 作者:行者123 更新时间:2023-12-05 08:37:57 25 4
gpt4 key购买 nike

我有一个简单的数据框:dput(emp)

structure(list(name = structure(1L, .Label = "Alex", class = "factor"), 
job = structure(1L, .Label = "", class = "factor"), Mgr = structure(1L, .Label = "", class = "factor"),
update = structure(18498, class = "Date")), class = "data.frame", row.names = c(NA,
-1L))

我想将所有空行转换为NULL

最简单的实现方式是:

emp[emp==""] <- NA

哪个当然可行,但我收到日期列的错误:

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

如何在不处理日期列的情况下将所有其他空行转换为 NULL?请注意,实际数据框有 30000 多行。

最佳答案

尝试将日期变量格式化为字符,进行更改并再次转换为日期:

#Format date
emp$update <- as.character(emp$update)
#Replace
emp[emp=='']<-NA
#Reformat date
emp$update <- as.Date(emp$update)

输出:

  name  job  Mgr     update
1 Alex <NA> <NA> 2020-08-24

关于r - 在 R 中将空值转换为 NULL - 处理日期列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64125153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com