Reputation: 141
I have a dataframe in which some of the numeric column are in factor and i want to convert to numeric value. However i tried the below code but still it is losing the information.
> str(xdate1)
'data.frame': 6 obs. of 1 variable:
$ Amount.in.doc..curr.: Factor w/ 588332 levels "-0.5","-1","-1,000",..: 5132 57838 81064 98277 76292 71982
After converting to numeric i am losing the information. below is the output:
> xdate1$Amount.in.doc..curr.<-as.numeric(as.character(xdate1$Amount.in.doc..curr.))
Warning message:
NAs introduced by coercion
> str(xdate1)
'data.frame': 6 obs. of 1 variable:
$ Amount.in.doc..curr.: num -150 NA NA NA NA NA
Upvotes: 0
Views: 50
Reputation: 388817
You have values with commas( ','
) which turn into NA
when changing to numeric, remove them before converting to numeric.
xdate1$Amount.in.doc..curr. <- as.numeric(gsub(',', '', xdate1$Amount.in.doc..curr.))
Or use parse_number
from readr
xdate1$Amount.in.doc..curr. <- readr::parse_number(as.character(xdate1$Amount.in.doc..curr.))
Upvotes: 2