【发布时间】:2026-01-20 10:40:01
【问题描述】:
我希望 as.Date 函数将任何与给定格式(下面的 dateFormat)不匹配的值添加到空白数据框中。目前,它将它们全部转换为NA。我们根本不希望它转换只是输出到数据框。有谁知道此时如何短路as.Date 功能?
dataValues = data.frame(id = c("a1", "a2", "a4", "a5", "a6","a7", "a8", "a9", "a10", "a11", "a12","a13", "a14", "a15", "a16", "a17"),
value1 = c('10/3/2012', '13/4/2012', NA, '0', '1/2/2012', '2/30/2013',
'2/4/2012', "N/A", 'No Data', '5-6-2012', '2/5/2012',
'Not Applicable', '5/8/2013', '2/5/2014', '6/9/2010', '5/4/2014'),
stringsAsFactors = FALSE)
dateFormat = "%m/%d/%Y"
dates = toString(dataValues[,2])
tempSplit = unlist(strsplit(dates,","))
#If it encounters anything that is not valid for the format
#such as out of range or incorrect format it will change the value
#to NA in the data frame.
dates = as.data.frame(as.Date(tempSplit, dateFormat))
names(dates) = c("Date")
【问题讨论】:
-
为什么不采用日期数据框为 NA 的原始数据框中的值。 dtaValues[is.na(日期), ]