How to make a vector (saved as a string) a vector back again?

I saved polygon coordinates to the database and this is how it's saved to a table:

c(22.841941, 23.413109, 21.831413, 22.841941, 50.134664, 49.453843, 49.353756, 50.134664)

Now, I need to read it to R and make it polygon again but the issue is that when I read coordinates from database then they are of type character (with quotation marks):

"c(22.841941, 23.413109, 21.831413, 22.841941, 50.134664, 49.453843, 49.353756, 50.134664)"   

I wanted to make it vector again c(22.841941, 23.413109,...) - remove somehow the quotations marks. I tried some string operations e.g.:

text <- "c(22.841941, 23.413109, 21.831413, 22.841941, 50.134664, 49.453843, 49.353756, 50.134664)" 
stringr::str_replace(text, "\""," ")

or:

gsub('"',"",text)

but it doesn't remove quotation marks. And sure, I also tried to make it a list by list(text) or numeric as.numeric(text) but all of these operations fail. How can I make this string-vector a vector (or matrix/list, whatever) again?



Read more here: https://stackoverflow.com/questions/67940917/how-to-make-a-vector-saved-as-a-string-a-vector-back-again

Content Attribution

This content was originally published by mustafa00 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: