我有一个数据框,其中一列是(应该是)00:00:00.0 yyyy-mm-dd
格式的日期。大多数条目是,但有些不是。有没有办法删除包含非日期的行?类似的东西(如果该列是“DATE”)
data <- data[is.Date(DATE)==TRUE,]
例如。
Fruit Date
apple 00:00:00.0 2005-02-01
pear 00:00:00.0 2006-02-01
orange 00:00:00.0 -8-2-402145
rhino 00:00:00.0 2003-04-21
我要
Fruit Date
apple 00:00:00.0 2005-02-01
pear 00:00:00.0 2006-02-01
rhino 00:00:00.0 2003-04-21
最佳答案
按照 joran 的推理:
# get the test data
test <- data.frame(
Fruit=c("apple","pear","orange","rhino"),
Date=c("00:00:00.0 2005-02-01",
"00:00:00.0 2006-02-01",
"00:00:00.0 -8-2-402145",
"00:00:00.0 2003-04-21")
)
# remove the rows by checking if not (!) an NA due to not meeting the date format
test[!is.na(strptime(test$Date,format="00:00:00.0 %Y-%m-%d")),]
结果:
Fruit Date
1 apple 00:00:00.0 2005-02-01
2 pear 00:00:00.0 2006-02-01
4 rhino 00:00:00.0 2003-04-21
关于R删除条目不是日期的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12718892/