删除数据框中行的有效方法

时间:2014-01-09 17:55:33

标签: r dataframe

我有一个像这样的数据框

186740210700000000 | 1859-10-9 03:00:00 | SRID=4326;POINT(134.50 -37.50) | 5 | E178548E101850
183130210700000000 | 1860-10-9 11:00:00 | SRID=4326;POINT(133.50 -38.50) | 5 | E178548E101850
183130210700000001 | 1859-10-9 15:00:00 | SRID=4326;POINT(133.50 -38.50) | 5 | E178548E101850
183120210700000000 | 1859-10-9 19:00:00 | SRID=4326;POINT(132.50 -38.50) | 5 | E178548E101850
179520210700000000 | 1860-10-9 23:00:00 | SRID=4326;POINT(132.50 -39.50) | 5 | E178548E101850
179520210800000000 | 1859-10-10 03:00:00 | SRID=4326;POINT(132.50 -39.50) | 5 | E178548E101850

我想要做的是取消那些行,例如年份1860,然后恢复数据框的外观,如上所示。哪种方式最好?

非常感谢

2 个答案:

答案 0 :(得分:2)

您可以使用grepl

在第二列中搜索模式
dat[!grepl("1860", dat[ , 2]), ]

这将排除在第2列中可以找到1860的所有行。

结果:

            V1                    V2                               V3 V4              V5
1 1.867402e+17   1859-10-9 03:00:00   SRID=4326;POINT(134.50 -37.50)   5  E178548E101850
3 1.831302e+17   1859-10-9 15:00:00   SRID=4326;POINT(133.50 -38.50)   5  E178548E101850
4 1.831202e+17   1859-10-9 19:00:00   SRID=4326;POINT(132.50 -38.50)   5  E178548E101850
6 1.795202e+17  1859-10-10 03:00:00   SRID=4326;POINT(132.50 -39.50)   5  E178548E101850

答案 1 :(得分:0)

您也可以按照@TARehman的建议使用POSIX日期/时间类:

df[,2] <- as.POSIXct(df[,2], format="%Y-%m-%d %H:%M:%S")
(df[as.POSIXlt(df[,2])$year != 1860 - 1900,])
#             V1                  V2                               V3 V4              V5
# 1 1.867402e+17 1859-10-09 03:00:00  SRID=4326;POINT(134.50 -37.50)   5  E178548E101850
# 3 1.831302e+17 1859-10-09 15:00:00  SRID=4326;POINT(133.50 -38.50)   5  E178548E101850
# 4 1.831202e+17 1859-10-09 19:00:00  SRID=4326;POINT(132.50 -38.50)   5  E178548E101850
# 6 1.795202e+17 1859-10-10 03:00:00  SRID=4326;POINT(132.50 -39.50)   5  E178548E101850


df[,1] <- format(df[,1], scientific=F)
cat(paste(apply(df, 1, paste, collapse=" | "), collapse="\n"))
# 186740210700000000 | 1859-10-09 03:00:00 |  SRID=4326;POINT(134.50 -37.50)  | 5 |  E178548E101850
# 183130210700000000 | 1860-10-09 11:00:00 |  SRID=4326;POINT(133.50 -38.50)  | 5 |  E178548E101850
# 183130210700000000 | 1859-10-09 15:00:00 |  SRID=4326;POINT(133.50 -38.50)  | 5 |  E178548E101850
# 183120210700000000 | 1859-10-09 19:00:00 |  SRID=4326;POINT(132.50 -38.50)  | 5 |  E178548E101850
# 179520210700000000 | 1860-10-09 23:00:00 |  SRID=4326;POINT(132.50 -39.50)  | 5 |  E178548E101850
# 179520210800000000 | 1859-10-10 03:00:00 |  SRID=4326;POINT(132.50 -39.50)  | 5 |  E178548E101850