Reputation: 2460
I have a column in a dataframe that has geocoordinate values that get converted to a factor on import that look like this
[ [ -106.2432752 , 39.0077354]]
[ [ -106.1867662 , 38.8326113]]
I want to convert them to a list of numbers but cant figure out a way. I've tried removing the brackets with gsub but it returns strings.
Upvotes: 1
Views: 268
Reputation: 837
Try it. gregexpr
locates where is the pattern. Then regmatches
extracts the patterns it returns a list. Bind those lists by row and convert them with as.numeric
function in a apply
.
> tmp
V1
1 [ [ -106.2432752 , 39.0077354]]
2 [ [ -106.1867662 , 38.8326113]]
apply(do.call(rbind,regmatches(as.character(tmp[,1]),gregexpr("[-]*[0-9]*[.]{1}[0-9]+",as.character(tmp[,1])))),2,as.numeric)
[,1] [,2]
[1,] -106.2433 39.00774
[2,] -106.1868 38.83261
Upvotes: 1