Deleting rows in a dataset goes wrong

I have the following dataset:

text <- c(1:13)
numbers <- c(1,1,1,1,1,1,1,1,1,1,1,1,1)
test <- data.frame(
    text =text,
    is.numeric.feature = numbers)

   text is.numeric.feature
1     1                  1
2     2                  1
...
13    13                 1

Now I want to delete all rows where the numeric function == 0 (they are not here, but there are other data sets) When I use the following command, my complete data set is empty, what did I do wrong?

test[-c(which(test$is.numeric.feature==0)),]
+3
source share
3 answers

The reason is that it which(data$is.numeric.feature==0)returns integer(0)when there are no zeros.

> Data[-integer(0),]
[1] text               is.numeric.feature
<0 rows> (or 0-length row.names)

To overcome this, it is better to work with logical vectors:

Data[Data$is.numeric.feature!=0,]

The side channel c()in your oneliner is redundant. whichreturns a vector anyway. And please, never give your frameworks or vectors a name, which is also a function name. You will have problems at some point.

+3

.

data[!data$is.numeric.feature == 0, ]
+2

This happens incorrectly because the operator whichreturns an integer (0), an empty integer vector. Indexing is -numeric(0)not interpreted as “omit nothing”, but as indexing integer(0), which means “index nothing”. I think this should go right if there is at least one zero in your data.

But you do not need, one way or another, and the logical vector is fine. These both work:

data[data$is.numeric.feature!=0,]

subset(data,is.numeric.feature!=0)
0
source

All Articles