删除数据框中的特定行

时间:2012-08-28 00:40:20

标签: r dataframe

我有一个如下所示的数据框:

            type   created_at repository_name
1        IssuesEvent 3/11/12 6:48       bootstrap
2        IssuesEvent 3/11/12 6:48       bootstrap
3        IssuesEvent 3/11/12 6:48       bootstrap
4        IssuesEvent 3/11/12 6:52       bootstrap
5        IssuesEvent 3/11/12 6:52       bootstrap
6        IssuesEvent 3/11/12 6:52       bootstrap
7  IssueCommentEvent 3/11/12 7:03       bootstrap
8  IssueCommentEvent 3/11/12 7:03       bootstrap
9  IssueCommentEvent 3/11/12 7:03       bootstrap
10       IssuesEvent 3/11/12 7:03       bootstrap
11       IssuesEvent 3/11/12 7:03       bootstrap
12       IssuesEvent 3/11/12 7:03       bootstrap
13        WatchEvent 3/11/12 7:15       bootstrap
14        WatchEvent 3/11/12 7:15       bootstrap
15        WatchEvent 3/11/12 7:15       bootstrap
16        WatchEvent 3/11/12 7:18        hogan.js
17        WatchEvent 3/11/12 7:18        hogan.js
18        WatchEvent 3/11/12 7:18        hogan.js
19        WatchEvent 3/11/12 7:19       bootstrap

这是一个dput():

structure(list(type = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 1L, 
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("IssueCommentEvent", 
"IssuesEvent", "WatchEvent"), class = "factor"), created_at = structure(c(1L, 
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 
5L, 6L), .Label = c("3/11/12 6:48", "3/11/12 6:52", "3/11/12 7:03", 
"3/11/12 7:15", "3/11/12 7:18", "3/11/12 7:19"), class = "factor"), 
    repository_name = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L), .Label = c("bootstrap", 
    "hogan.js"), class = "factor")), .Names = c("type", "created_at", 
"repository_name"), class = "data.frame", row.names = c(NA, -19L
))

我想在'type'列中删除包含字符串'WatchEvent'的每一行。如何在R中完成此操作?

1 个答案:

答案 0 :(得分:3)

df <- structure(list(type = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 1L, 
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("IssueCommentEvent", 
"IssuesEvent", "WatchEvent"), class = "factor"), created_at = structure(c(1L, 
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 
5L, 6L), .Label = c("3/11/12 6:48", "3/11/12 6:52", "3/11/12 7:03", 
"3/11/12 7:15", "3/11/12 7:18", "3/11/12 7:19"), class = "factor"), 
    repository_name = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L), .Label = c("bootstrap", 
    "hogan.js"), class = "factor")), .Names = c("type", "created_at", 
"repository_name"), class = "data.frame", row.names = c(NA, -19L
))

df_a <- df[df$type!="WatchEvent",]

#                 type   created_at repository_name
# 1        IssuesEvent 3/11/12 6:48       bootstrap
# 2        IssuesEvent 3/11/12 6:48       bootstrap
# 3        IssuesEvent 3/11/12 6:48       bootstrap
# 4        IssuesEvent 3/11/12 6:52       bootstrap
# 5        IssuesEvent 3/11/12 6:52       bootstrap
# 6        IssuesEvent 3/11/12 6:52       bootstrap
# 7  IssueCommentEvent 3/11/12 7:03       bootstrap
# 8  IssueCommentEvent 3/11/12 7:03       bootstrap
# 9  IssueCommentEvent 3/11/12 7:03       bootstrap
# 10       IssuesEvent 3/11/12 7:03       bootstrap
# 11       IssuesEvent 3/11/12 7:03       bootstrap
# 12       IssuesEvent 3/11/12 7:03       bootstrap

删除行是与csv相关的单独过程:

write.csv(df_a, "no_WatchEvent.csv", row.names=FALSE)
相关问题