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 remove all rows where the numeric feature == 0 (there are none here, but in other datasets there are)
When I use the following command, my complete dataset is empty, what did I do wrong?
test[-c(which(test$is.numeric.feature==0)),]
The reason is that
which(data$is.numeric.feature==0)returnsinteger(0)when there are no zeros.To overcome this, better work with logical vectors :
On a sidenote, the
c()in your oneliner is redundant.whichreturns a vector anyway. And please, never ever give your dataframe or vectors a name that’s also the name of a function. You will run into trouble at one point.