在不知道行号的情况下删除特定行

时间:2013-04-02 22:34:37

标签: r

我想从数据框中删除一行并对结果列求和。我根据其内容知道要删除的行,但不知道它的行号。下面我将介绍三个例子,其中两个有效。使用-删除行仅适用于要删除第一行的情况。这是为什么?

我的问题与此类似:How to delete the first row of a dataframe in R?但是,根据行号删除行。

# This works.

state = 'OH'

my.data = read.table(text = "
      county  y1990 y2000
        cc       NA    2
        OH       NA   10
        bb       NA    1
", sep = "", header = TRUE, na.strings = "NA", stringsAsFactors = FALSE)

my.colsums2 <- colSums(my.data[!(my.data$county == state), 2:ncol(my.data)], na.rm=TRUE)
my.colsums2

# y1990 y2000 
#    0     3

# This works.

my.data = read.table(text = "
      county  y1990 y2000
        OH       NA   10
        cc       NA    2
        bb       NA    1
", sep = "", header = TRUE, na.strings = "NA", stringsAsFactors = FALSE)

my.colsums2 <- colSums(my.data[-(my.data$county == state), 2:ncol(my.data)], na.rm=TRUE)
my.colsums2

# y1990 y2000 
#    0     3

# This does not work.

my.data = read.table(text = "
      county  y1990 y2000
        cc       NA    2
        OH       NA   10
        bb       NA    1
", sep = "", header = TRUE, na.strings = "NA", stringsAsFactors = FALSE)

my.colsums2 <- colSums(my.data[-(my.data$county == state), 2:ncol(my.data)], na.rm=TRUE)
my.colsums2

# y1990 y2000 
#    0    11

我想我仍然对!-之间的区别感到困惑。谢谢你的任何建议。

2 个答案:

答案 0 :(得分:6)

这应该清除-!之间的差异,我怀疑你可以从那里拿走它;)

my.data$county == state
# [1]  TRUE FALSE FALSE

!(my.data$county == state)
# [1] FALSE  TRUE  TRUE

-(my.data$county == state)
# [1] -1  0  0
否定布尔值的

!是您应该在此处使用的运算符。

答案 1 :(得分:3)

我认为记住你在做什么很重要。当您将条件参数传递给行或列的子集时,它需要是全长TRUE或FALSE测试,或者它必须是表示行(或列)的数字。

这是一个带矢量的简单示例。尝试在控制台中输入条件以查看它们提供的内容

试试这些:

x <- rnorm(20)

## These use integer values for indexing
x[which(x > 1)]  # Numbers > Only those numbers which match

## These use logical values for indexing
x[x > 1]    # Logical > Only those that are true
x[!(x < 1)] # Logical > Only those that are false

不良行为:

x[-which(x > 1)] # Positive numbers to negative numbers = BAD
x[!which(x > 1)] # Converts numbers to logical = BAD
x[-(x > 1)] # Converts logical to numeric = BAD

特定于您的示例:

!(my.data$county == state) # Converts TRUE/FALSE to FALSE/TRUE
which(my.data$county != state) # Rows where my.data$count not equal state

就我个人而言,我建议在所有情况下使用which()以避免对逻辑或数字转换的潜在否定。它也更容易“翻译”