Ananya Vidyanathan
Ananya Vidyanathan

Reputation: 51

How to convert character in dataframe to numeric without the loss of decimal

Assume a database in the below format

Voltage Global_intensity Sub_metering_1
 <chr>   <chr>            <chr>         
1 234.840 18.400           0.000         
2 233.630 23.000           0.000         
3 233.290 23.000           0.000         
4 233.740 23.000           0.000         
5 235.680 15.800           0.000         
6 235.020 15.000           0.000         
7 235.090 15.800           0.000         
8 235.220 15.800           0.000         
9 233.990 15.800           0.000         
10 233.860 15.800           0.000         
# ... with 2,075,249 more rows

I want to convert this character variables of column types to numeric without loss of decimal digits

df1$Voltage <-as.double(df1$Voltage,options(digits = 8))
 Voltage Global_intensity Sub_metering_1
     <dbl> <chr>            <chr>         
 1    235. 18.400           0.000         
 2    234. 23.000           0.000         
 3    233. 23.000           0.000         
 4    234. 23.000           0.000         
 5    236. 15.800           0.000         
 6    235. 15.000           0.000         
 7    235. 15.800           0.000         
 8    235. 15.800           0.000         
 9    234. 15.800           0.000         
10    234. 15.800           0.000         
# ... with 2,075,249 more rows

Now I get a result like this with loss of decimal digits. How to rectify it?

Upvotes: 1

Views: 695

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269664

The key point is to distinguish between what is shown and what is stored. Voltage is still stored at full precision.

DF[] <- lapply(DF, as.numeric)
DF$Voltage
## [1] 234.84 233.63 233.29 233.74 235.68 235.02 235.09 235.22 233.99 233.86

Note

Lines <- "Voltage Global_intensity Sub_metering_1
1 234.840 18.400           0.000         
2 233.630 23.000           0.000         
3 233.290 23.000           0.000         
4 233.740 23.000           0.000         
5 235.680 15.800           0.000         
6 235.020 15.000           0.000         
7 235.090 15.800           0.000         
8 235.220 15.800           0.000         
9 233.990 15.800           0.000         
10 233.860 15.800           0.000"

library(tibble)
DF <- as_tibble(read.table(text = Lines, colClasses = "character"))

Upvotes: 2

Related Questions