ashes
ashes

Reputation: 35

conversion to float in R

I have a set of Latitudes stored in VARCHAR format in mysql database-

"[26.455359183496455,26.44229519242908,26.437069181137474,26.45489812668682]"

I have imported them in R and I want to work with them by converting them as float. I know that as.numeric() is used for conversion but how to apply it in my case. someone please guide me.

Upvotes: 3

Views: 47485

Answers (1)

MrFlick
MrFlick

Reputation: 206496

You can do this in three steps.

  1. remove the brackets (via gsub(),
  2. split on commas (via strsplit(),
  3. convert to numeric (via as.numeric()). For example

In code this looks like

input <- "[26.455359183496455,26.44229519242908,26.437069181137474,26.45489812668682]"
as.numeric(strsplit(gsub("(^\\[|\\]$)", "", input), ",")[[1]])

Upvotes: 6

Related Questions