Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201945

Rearrange GIS data of Origina Destinations to one row each point

$
0
0

I start with a "wide" data set of GIS data of Origin Destinations (OD), that I want to rearange into a longer data set of one row for each point. I already managed to melt and then dcast to rearange the first layer of the ODs, but I am strungling with the next step

My MWE is on example of a trip from Brussels to London in three legs. But there are many more IDs and they can have more or less legs.

library(data.table)

Start = c("Brussels","Lille","Dover")
Start_lon <- c(4.3570964,3.075685,1.3047866)
Start_lat <- c(50.845504, 50.6390876,51.12623)
Border = c("Baisieux", "Frethun","London")
Border_lon = c(3.075685,1.811221, -0.1244124)
Border_lat <- c(50.61848,  50.90148, 51.53165)

df <- data.table(ID = 1,
                 Sub_ID = 1:3,
                 Start = Start, 
                 Start_lon = Start_lon,
                 Start_lat,
                 Border = Border,
                 Border_lon = Border_lon,
                 Border_lat = Border_lat)

So i have one ID with three Sub_IDs each with two points and finally I would expect 6 rows for each ID for each station. I manage to expand the data from 3 to 6 rows so that one row is the start/origin point an the other the border/destination point indicated by the Type variable.

df_long <- melt(df, id.vars = c("ID", "Sub_ID", "Start", "Border"))
df_long <- df_long[, c("Type", "Coordinates") := tstrsplit(variable, "_", fixed=TRUE)]
df_long <- dcast(df_long, ID+Sub_ID+Start+Border+Type~Coordinates, value.var="value")

Now I do not know how to proceed to get into the structure of ID==1 and six Sub_Ids so that I would get 6 Rows with stations for
Brussels- Baisieux - Lill- Frethun - Dover- London

I was hoping to get something like this

df_goal <- data.table(ID = 1,
                      Sub_ID = 1:6,
                      Stop = c("Brussels","Baisieux","Lille", "Frethun", "Dover", "London"),
                      lat = NA,
                      lon = NA)

Maybe still with the information if the stop is a "Start" or a "Border"


Viewing all articles
Browse latest Browse all 201945

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>