删除观察

时间:2017-04-10 14:55:17

标签: r data-manipulation

这是非常基本的,但我现在已经坚持了一段时间。

我想从数据集hitters.txt 中移除观察 > dput(hitters[280:290,]) structure(list(AtBat = c(439L, 453L, 528L, 633L, 16L, 562L, 281L, 593L, 687L, 368L, 263L), Hits = c(96L, 103L, 122L, 210L, 2L, 169L, 76L, 152L, 213L, 103L, 70L), HmRun = c(0L, 8L, 1L, 6L, 0L, 17L, 3L, 23L, 10L, 3L, 1L), Runs = c(44L, 53L, 67L, 91L, 1L, 88L, 42L, 69L, 91L, 48L, 26L), RBI = c(36L, 33L, 45L, 56L, 0L, 73L, 25L, 75L, 65L, 28L, 23L), Walks = c(65L, 52L, 51L, 59L, 0L, 53L, 20L, 53L, 27L, 54L, 30L), Years = c(4L, 2L, 4L, 6L, 2L, 8L, 8L, 6L, 4L, 8L, 4L), CAtBat = c(711L, 507L, 1716L, 3070L, 28L, 3181L, 2658L, 2765L, 1518L, 1897L, 888L), CHits = c(148L, 123L, 403L, 872L, 4L, 841L, 657L, 686L, 448L, 493L, 220L), CHmRun = c(1L, 8L, 12L, 19L, 0L, 61L, 48L, 133L, 15L, 9L, 9L), CRuns = c(68L, 63L, 211L, 420L, 1L, 450L, 324L, 369L, 196L, 207L, 83L), CRBI = c(56L, 39L, 146L, 230L, 0L, 342L, 300L, 384L, 137L, 162L, 82L), CWalks = c(99L, 58L, 155L, 274L, 0L, 373L, 179L, 321L, 89L, 198L, 86L), League = structure(c(2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L), .Label = c("A", "N"), class = "factor"), Division = structure(c(1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 1L), .Label = c("E", "W"), class = "factor"), PutOuts = c(229L, 289L, 209L, 367L, 247L, 351L, 106L, 315L, 294L, 209L, 81L ), Assists = c(406L, 407L, 372L, 432L, 4L, 442L, 144L, 10L, 445L, 246L, 147L), Errors = c(22L, 6L, 17L, 16L, 8L, 17L, 7L, 6L, 13L, 3L, 4L), Salary = c(150, 105, 350, 90, NA, 530, 341.667, 940, 350, 326.667, 250), NewLeague = structure(c(2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L), .Label = c("A", "N"), class = "factor")), .Names = c("AtBat", "Hits", "HmRun", "Runs", "RBI", "Walks", "Years", "CAtBat", "CHits", "CHmRun", "CRuns", "CRBI", "CWalks", "League", "Division", "PutOuts", "Assists", "Errors", "Salary", "NewLeague"), row.names = c("-Steve Jeltz", "-Steve Lombardozzi", "-Spike Owen", "-Steve Sax", "-Tony Armas", "-Tony Bernazard", "-Tom Brookens", "-Tom Brunansky", "-Tony Fernandez", "-Tim Flannery", "-Tom Foley"), class = "data.frame")

hitters <- hitters[!hitters$Colname == "-Steve Sax",] 

如果我知道我将使用的第一列的名称:

hitters <- hitters[hitters$AtBat != "-Steve Sax", ]

read.table("hitters.txt", head = F)

但我不知道第一栏的名称:

。我试过了: read.table("hitters.txt", head = F) `和

head = T

我的问题是:

  1. 如何删除观察结果?
  2. 为什么{{1}}无效?

1 个答案:

答案 0 :(得分:1)

第一个&#34;列&#34;表示行名称(这不是数据集中的实际列,但在输出中显示如此)。您可以使用函数rownames

访问行名称
hitters[!rownames(hitters) %in% '-Steve Sax', ]

将从数据集中提取观察结果。

输出:

                  AtBat Hits HmRun Runs RBI Walks Years CAtBat CHits CHmRun CRuns
-Steve Jeltz         439   96     0   44  36    65     4    711   148      1    68
-Steve Lombardozzi   453  103     8   53  33    52     2    507   123      8    63
-Spike Owen          528  122     1   67  45    51     4   1716   403     12   211
-Tony Armas           16    2     0    1   0     0     2     28     4      0     1
-Tony Bernazard      562  169    17   88  73    53     8   3181   841     61   450
-Tom Brookens        281   76     3   42  25    20     8   2658   657     48   324
-Tom Brunansky       593  152    23   69  75    53     6   2765   686    133   369
-Tony Fernandez      687  213    10   91  65    27     4   1518   448     15   196
-Tim Flannery        368  103     3   48  28    54     8   1897   493      9   207
-Tom Foley           263   70     1   26  23    30     4    888   220      9    83