Title: Delete rows with negative values
Tags: r;row
Question: In R I am trying to delete rows within a dataframe (ants) which have a negative value under the column heading Turbidity. I have tried
ants<-ants[ants$Turbidity<0,]
but it returns the following error:
Warning message:
In Ops.factor(ants$Turbidity, 0) : < not meaningful for factors
Any ideas why this may be? Perhaps I need to make the negative values
NA before I then delete all NAs?
Any ideas much appreciated, thank you!
@Joris: result is
str(ants$Turbidity)
num [1:291] 0 0 -0.1 -0.2 -0.2 -0.5 0.1 -0.4 0 -0.2 ...
Comment: Yes you are quite right, my mistake, one of many I think!

Comment: I'm think you meant `>` when writing `<`.

Comment: I meant the original ants$Turbidity, not the converted one. The one you give here can't give that error. But as you indicated yourself, using `header=T` seems to solve it, so don't bother.

Here is the accepted answer: Marek is right, it's a data problem. Now be careful if you use [as.numeric(ants$Turbidity] , as that one will always be positive. It gives the factor levels (1 to length(ants$Turbidity)), not the numeric factors.
Try this :
tt <- as.numeric(as.character(ants$Turbidity))
which(!is.na(tt))
It will give you a list of indices where the value was not numeric in the first place. This should enable you to first clean up your data.
&gt; Turbidity <- factor(c(1,2,3,4,5,6,7,8,9,0,"a"))
&gt; tt <- as.numeric(as.character(Turbidity))
Warning message:
NAs introduced by coercion
&gt; which(is.na(tt))
[1] 11
You shouldn't use the as.numeric(as.character(...)) structure to convert problematic data, as it will generate NA's that will mess with the rest. Eg:
&gt; Turbidity[tt &gt; 5]
[1] 6    7    8    9    <NA&gt;
Levels: 0 1 2 3 4 5 6 7 8 9 a
Comment for this answer: Thank you Joris and Marek. I have run this as suggested and indeed all of my rows are returned as character, at least none are returned as numeric. Now I have established that my data are in completely the wrong format, can you suggest how I can change them to numeric values or is it not that simple? Many thanks.
Comment for this answer: @Joey: So you mean that after which(!is.na(tt)) you get a list from 1 to the number of rows? If so, could you put the output of `str(ants$Turbidity)` in your question?
Comment for this answer: huh? That's a numeric vector. You can't get an error from that as you described. But you indicated yourself that the problem was the column headers, so using the option `header=T` solved the problem then I guess.

Here is another answer: EDIT. I forget about as.character conversion (see Joris comment).
Message mean that ants$Turbidit is a factor. It will work when you do
ants <- ants[as.numeric(as.character(ants$Turbidity)) &gt; 0,]
ants <- subset(ants, as.character(as.numeric(Turbidity)) &gt; 0)
But the real problem is that your data are not prepared to analysis. Such conversion should be done in the beginning. You should be careful cause there could be non-numeric values also.
Comment for this answer: Thank you, this works well, I realised I want to actually want to keep everything greater than or equal to 0.0. When you say this type of conversion should be done at the beginning...at the beginning of what? Sorry, I'm fairly new to R and not used to some of the best practice...thanks :)
Comment for this answer: @Joris Obvious thing but I missed it. Thx.
Comment for this answer: `as.numeric` will give factor levels, not the numeric value. If you use `as.numeric(as.character(ants$Turbidity))`, you'll get problems with` NA`.

Here is another answer: This should also work using the tidyverse (assuming column is the correct data type).
ants %&gt;% dplyr75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0ilter(Turbidity &gt;= 0)

Here is another answer: Always do summary(ants) after reading in data, and check if you get what you expect.
It will save you lots of problems. Numeric data is prone to magic conversion to character or factor types.
Comment for this answer: Oh dear, I now see none of my columns of data (all numeric) have imported as numeric. I feel I have missed something fundamental when importing them. How do I specify that I want certain columns to be either Date, Time or numeric please?
Comment for this answer: Just thought, could it be that my header row is causing the conversion? I have re-read in the data specifying column names this time, which I think has gotten rid of the non-numeric characters. I tried the initial arguement again in my original question and no error shows. It returns all those values larger than zero. Does anyone know how I can adjust this to include zero values please?
Comment for this answer: Great, thank you everyone, especially Joris! So, does anyone know the acceptable syntax for a "equal to or greater than" symbol in R. Right now I have just greater than >, I want >= but this isn't recognised?
Comment for this answer: Sorry, one more thing...if it was a range of values I wanted to keep, i.e. >= 0.9 and 3&x<5]
Comment for this answer: @Joey How you read them? Maybe you choose wrong version of `read.csv`? See help to `read.table` and description of `colClasses` argument.
Comment for this answer: @Spacedman I usually use `str` cause on large datasets `summary` could be overkill.
Comment for this answer: @Spacedman: there is nothing magic to the conversion. If it gets converted to character or factor types, there is a problem in the data in the first place.
Comment for this answer: @Joey : the conversion is due to the fact that there are non-numeric characters in your dataset. If it would be possible, R would have made them numeric. You have to remove those non-numeric characters.
Comment for this answer: @Joey : >= should work. Sure you didn't try => ? (the latter one gives an error)

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 4, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2010:00:11/18/10', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<r><row>', 'Title': 'Delete rows with negative values', 'ViewCount': 13722, 'id': 4213943, 'user_AboutMe': '', 'user_AccountId': 211739.0, 'user_DisplayName': 'Joey', 'user_Reputation': 181.0, 'user_Views': 13.0, 'user_id': 464104.0}}