【问题标题】:replacing NA's in a Large POSIXct with Sys.time()用 Sys.time() 替换大型 POSIXct 中的 NA
【发布时间】:2018-06-08 14:32:56
【问题描述】:

我有大约 70,000 个元素的 large POSIXct

resolutionDate <- c(as.POSIXct(data$Resolution.Date, format = '%b %d, %Y'))

上面的代码将值从Jun 5, 2018 3:21 PM 更改为2018-06-05

但是,有些值是 NA,我想将所有 NA 替换为 Sys.time(),作为今天的日期。

我尝试使用replace() 方法, replace(resolutionDate, if(resolutionData == "NA"), Sys.time())

但是没有用..

我该怎么做?

【问题讨论】:

  • 我认为如果所有值的格式都相同,应该是format = '%b %d, %Y %I:%M %p'
  • 使用ifelse(is.na(resolutionData$resolutionDate), Sys.time(), resolutionData$resolutionDate),使用is.na而不是== ,请注意akrun先前评论中指出的日期格式

标签: r posixct posixlt


【解决方案1】:

这样的?

# generate time vector
a <- as.POSIXct(1:70000,origin="1970-01-01")
# replace the 5th with a NA value and show first 10 elements
a[5] <- NA
a[1:10]
# replace all na values with the current system time
a[is.na(a)] <- Sys.time()
# show result
a[1:10]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-06
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    相关资源
    最近更新 更多