0

I am attempting to use read.csv() to read in a .csv file, but three of my columns contain floating point values. R coerces these into factors, but I would like them to retain their original values so I can accurately compare them to one another. I've tried to read the documentation, but the only thing I see there is the option to set stringsasfactors = FALSE. Then I retain the decimal places in my column elements, but they are not numbers that I can compare to one another.

For example, if my column contained the values 3.1, 4.2, 5.3, R would coerce these into factors. If I calls as.numeric() on them, they are squashed to 3, 4, 5. How can I keep them as floating point values when I read them in?

  • 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) ? If R is coercing a column to factor, that's because there's something else in that column that can't be interpreted as numeric. And if you do want to convert you need `as.numeric(as.factor(x))` (this is an R FAQ). – Ben Bolker Oct 27 '16 at 22:57
  • Sorry I didn't reply to this one. I've been grinding away on other functions in the meantime and was under the wire. Thanks for your help, though! –  Oct 29 '16 at 01:51

1 Answers1

0

My experience is that you need to as.numeric(as.character(data)). This is because a factor is listed as a string, that is identified as specific and repeating. All letters, numbers and symbols are considered characters within factors.

Going straight to a number, there is a chance that some part, such as the period, which is not a decimal place in a factor character string interrupts the conversion. It rounded in this case. Try dropping it out of factor into character, then to numeric!

sconfluentus
  • 4,693
  • 1
  • 21
  • 40
  • This was exactly it! I wouldn't have thought to take it to character and THEN to numeric. Thanks for your help. –  Oct 29 '16 at 01:50