How to change from factor to numeric without errors

Viewed 64

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?

1 Answers

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

It should work, this will prevent scientific notation.

Related