Let's say I have data where the column name is the city and row name is the longitude and latitude.
Columbus Nashville Austin Washington D.C. London Manchester
lon -82.99879 -86.7816 -97.74306 -77.03687 -0.1277583 -2.242631
lat 39.96118 36.16266 30.26715 38.90719 51.50735 53.48076
Is there a way that I can reformat this so I have:
City lon lat
Columbus 82.99879 39.96118
Nashville -86.7816 36.16266
The code to generate the data:
df <- structure(
list(
Columbus = structure(
list(lon = -82.9987942, lat = 39.9611755), .Names = c("lon", "lat")),
Nashville = structure(
list(lon = -86.7816016, lat = 36.1626638), .Names = c("lon", "lat")),
Austin = structure(
list(lon = -97.7430608, lat = 30.267153), .Names = c("lon", "lat")),
`Washington D.C.` = structure(
list(lon = -77.0368707, lat = 38.9071923), .Names = c("lon", "lat")),
London = structure(
list(lon = -0.1277583, lat = 51.5073509), .Names = c("lon", "lat")),
Manchester = structure(
list(lon = -2.2426305, lat = 53.4807593), .Names = c("lon", "lat"))),
.Names = c("Columbus", "Nashville", "Austin", "Washington D.C.",
"London", "Manchester"),
row.names = c("lon", "lat"), class = "data.frame")
My final goal is to map it but with the original format, it does not work.
I have tried:
tibble::rownames_to_column(as.data.frame(df))
data.table::setDT(as.data.frame(df))[]
And then to map:
leaflet(data = df) %>%
addProviderTiles("Thunderforest.OpenCycleMap") %>%
addMarkers(~lon, ~lat)
1
2
3
4
5
6