我希望as.Date
函数rbind
将任何与给定格式(下面的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")
答案 0 :(得分:0)
如果我理解正确,您可以使用split
分隔有效和无效日期:
split(dataValues,is.na(as.Date(dataValues$value1,dateFormat)))
$`FALSE`
id value1
1 a1 10/3/2012
5 a6 1/2/2012
7 a8 2/4/2012
11 a12 2/5/2012
13 a14 5/8/2013
14 a15 2/5/2014
15 a16 6/9/2010
16 a17 5/4/2014
$`TRUE`
id value1
2 a2 13/4/2012
3 a4 <NA>
4 a5 0
6 a7 2/30/2013
8 a9 N/A
9 a10 No Data
10 a11 5-6-2012
12 a13 Not Applicable