-2

I'm having a little problem, I need to use the "less than"(<) operator to a column, but my data.frame has that column(result) as a factor column, so I tried a few things to convert that to numeric values, here's an example of my data.frame

Data       result           otherData          MoreData
  x          403.3             low                old
  y          405.5             mid                older
  z          32                very low           young
  w          326.72            low                median
  t          45                low                alter
  g          56.37             low                ego

So, the rows with more than one number after the point, in this example are rows 4 and 6 with the values 326.72 and 56.37 become NA. This is what I tried:

auxUnit<-joined4[(as.numeric(as.character(joined4$result))),]

It worked just for the elements like "x" and "x.x", as I said, "x.xy" convert to NA, and it gives me this warning:

NAs introduced by coercion

And the output dataset is:

Data       result           otherData          MoreData
  x          403.3             low                old
  y          405.5             mid                older
  z          32                very low           young
  NA         NA                NA                 NA
  t          45                low                alter
  NA         NA                NA                 NA

I tried this code to cast to numeric because I need them to be numbers such as float or double in Java, so I can compare with a number, to filter the cases, for example to do something like:

auxUnit<-joined4[joined4$result>13.64,]

Is there a fast way to avoid this? Thanks in advance!

Amnor
  • 380
  • 6
  • 21
  • 1
    Can you please include data and/or code that will provide us with a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) ? (Results of `str()` or `dput()` ...) Also, maybe just `auxUnit<-as.numeric(as.character(joined4$result))` ? – Ben Bolker Jun 10 '16 at 12:43
  • 1
    Please, provide some data. Your `auxUnit<-...` line doesn't make much sense and it's not very likely what you are trying to do. You are supposed to provide integer indices indicating which rows you want to select; you are giving some `double` numbers that might well be beyond the size of the dataset. – nicola Jun 10 '16 at 12:43
  • I'll edit for more info, sorry about the delay in my answer, I was out of home the weekend. – Amnor Jun 13 '16 at 08:43

1 Answers1

0

Ok, it's solved, I hadn't realized that in my data.frame, some of the data had "," instead of "." as the decimal separator, I changed that with gsub like this:

joined4$result<-gsub( ",", ".", joined4$result )

Then it worked fine just with the "as.numeric" part.

Thanks for your comments!

Amnor
  • 380
  • 6
  • 21