Search code examples
rdata-cleaning

R avoid coercion with Factors


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!


Solution

  • 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!