Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conversion to float in R

Tags:

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.

like image 479
ashes Avatar asked Sep 12 '25 09:09

ashes


1 Answers

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]])
like image 195
MrFlick Avatar answered Sep 14 '25 02:09

MrFlick