2

I am trying to change the class of a column in a dataframe from factor to numeric, however, the values change:

Plot     DWT
1         1.29
2         0.82
3         1.21
4         3.16


Site.copy$DWT<-as.numeric(as.character(Site.copy$DWT))

#or

Site.copy$DWT<-as.numeric(levels(Site.copy$DWT))[as.integer(Site.copy$DWT)]

#Both codes result in this:

Plot     DWT
1        1.290000e+00
2        8.200000e+01
3        1.210000e+00
4        3.160000e+00

How do I fix this?

Cecilia P
  • 29
  • 2
  • 1
    This looks like printing behavior. `options(digits = 7)` is the default, but apparently that's been overridden. To understand why the values "change", see the FAQ on [Why are these numbers not equal?](https://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal) – Gregor Thomas Jun 27 '19 at 20:34

1 Answers1

2

You have to change the options in R. To do so : options(scipen = 999).

It should work, this will prevent scientific notation.

Gainz
  • 1,721
  • 9
  • 24
  • I tried that but it still appears differently: 1.2900000000; 0.8200000000; 1.2100000000;... – Cecilia P Jun 27 '19 at 20:11
  • @CeciliaP Is the output the same as in your question? – Gainz Jun 27 '19 at 20:12
  • Yes, the only difference is that the values do not have the "e+00" or "e+01" – Cecilia P Jun 27 '19 at 20:14
  • Indeed. Is it not what you want? 3.160000e+00 to be 3.16? If you get 3.160000 you simply have to use 2 digits. – Gainz Jun 27 '19 at 20:18
  • 1
    @CeciliaP See Gregor comment under your question. For some reasons your R ``options(digits = )`` is not default. R gives me the good output ``3.160000e+00 to 3.16``. – Gainz Jun 27 '19 at 21:06