2012-08-28 83 views
4

我有一個看起來像這樣的數據幀:刪除特定行的數據幀

  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 
)) 

我想刪除包含每一行。字符串「WatchEvent」中列‘型’我怎樣才能做到這一點R中

+0

如果您只是想從csv文件中刪除這些行,R可能不是最好的工具。你真的關心csv文件中的內容,或者只是data.frame中的內容?通常情況下,保持原始文件不變並將R中的數據進行子集更合理。 –

+0

您是對的。我想更改data.frame。我需要做任何不同的事情來改變data.frame嗎? – histelheim

+0

nope,所有你需要的是@AndyGarcia的答案:'df_a < - df [df $ type!=「WatchEvent」,]'。我會編輯你的問題來反映這一點。我會將其標記爲重複 - 但快速搜索不會返回任何重複項 - 儘管許多人使用此方法。 –

回答

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) 
+0

謝謝!這樣可行! – histelheim