2

我有一个数据框,其中一列是(意味着)表单中的日期00:00:00.0 yyyy-mm-dd。大多数条目是,但有些不是。有没有办法删除包含非日期的行?类似的东西(如果列是“日期”)

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
4

1 回答 1

3

按照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
于 2012-10-04T01:30:06.477 回答