R删除条目不是日期的行

时间:2012-10-04 00:42:05

标签: r date

我有一个数据框,其中一列是(意为)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

1 个答案:

答案 0 :(得分:3)

遵循乔兰的推理:

# 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