Ryan
Ryan

Reputation: 1432

Convert numerical character strings with multiple decimals to numeric data type in R

I have a variable with 8 million or so values in my data frame that are formatted like this: 1.275.900.0 (character data type). Changing them to factors is a no go because it slows down processing, so I need numerics.

I tried getting rid of the decimals and performing data type coercion with . . .

df$variable = as.numeric(sub(".", "", df$variable, fixed=TRUE))

Unfortunately, I got Warning message: NAs introduced by coercion.

How do I get around this?

Upvotes: 0

Views: 48

Answers (1)

ngwells
ngwells

Reputation: 188

As markus stated:

test<-"1.275.900.0"

as.numeric(gsub(".", "", test,fixed=T))

Should get you what you need.

Upvotes: 1

Related Questions