'How do I split the value of a list to 2 separate columns in R?
I used snscrape to get the geolocation of tweets for mapping them on the world map. The resulted coordinates come in format {'longitude': -70.7729481, 'latitude': 42.0616232}.
How do I split the list into two columns like?
| longitude | latitude |
|---|---|
| -70.7729 | 42.06162 |
I've tried unlist, read.table and few other ways I could find online but none of them work. It usually just ends up as
| V1 | V2 |
|---|---|
| {'longitude': -70.7729481 | 'latitude': 42.0616232} |
Solution 1:[1]
Looks like you deal with json data, you perhaps can do it right away from your json string.
library(jsonlite)
str <- '{"longitude" : -70.7729481, "latitude" : 42.0616232}'
data.frame(fromJSON(str))
# longitude latitude
# 1 -70.7729481 42.0616232
Solution 2:[2]
string <- "{'longitude': -70.7729481, 'latitude': 42.0616232}"
data.frame(reticulate::py_eval(string))
longitude latitude
1 -70.77295 42.06162
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Merijn van Tilborg |
| Solution 2 | onyambu |
