0

I'm using R in Azure databricks in Standard DS3 V2 . I have a R data frame, train, which looks like

head(train,5)
Loc  Date        Consumption
Lon  2020-12-01    47%
Lon  2020-12-02    30%
Lon  2020-12-03    50%
Lon  2020-12-04    50%
Lon  2020-12-04    50%

But the consumption column is factor data type. I'm trying to convert consumption column to numeric data type. So I wrote following code

train$Consumption1=as.numeric(train[, 3])/100

Divided it by 100 as before divide it was not coming fractional Now my data is looking like this

head(train,5)
Loc  Date        Consumption  Consumption1
Lon  2020-12-01    47%          0.44  
Lon  2020-12-02    30%          0.26
Lon  2020-12-03    50%          0.48
Lon  2020-12-04    50%          0.48 
Lon  2020-12-04    50%          0.48

Can you suggest me how to convert Consumption as numeric data without changing it's value?

newinPython
  • 313
  • 1
  • 6
  • 19
  • What does `class(train$Consumption)` return? Is your data stored as a factor? Are you using a version of R that's older that 4.0? – MrFlick Dec 24 '20 at 07:29
  • 1
    Most likely a duplicate of this answer in particular: https://stackoverflow.com/a/17435403/2372064 – MrFlick Dec 24 '20 at 07:31
  • Maybe a better duplicate (assuming it's a factor) https://stackoverflow.com/questions/48005566/factor-with-comma-and-percentage-to-numeric – MrFlick Dec 24 '20 at 07:32
  • A more canonical duplicate https://stackoverflow.com/questions/3418128/how-to-convert-a-factor-to-integer-numeric-without-loss-of-information (doesn't address the "%" issue though). The "%" makes R think the value is not a number. – MrFlick Dec 24 '20 at 07:33

0 Answers0