Skip to content

Instantly share code, notes, and snippets.

@Torenable
Created January 26, 2017 02:40
Show Gist options
  • Save Torenable/191d1bb0d4b45c102113edb28b016930 to your computer and use it in GitHub Desktop.
Save Torenable/191d1bb0d4b45c102113edb28b016930 to your computer and use it in GitHub Desktop.
Convert the list of adjacent states to a data frame of directed state pairs
[List of Adjacent States in US](https://writeonly.wordpress.com/2009/03/20/adjacency-list-of-states-of-the-united-states-us/)
/*
glench provides a JSON file, and the URL is https://gist.github.com/3906059
Here is a R code to convert the nested list to a paired data frame in R
*/
library(dplyr)
lst <- jsonlite::read_json("path/to/the_list.json")
# pair the name of the list with every element
tmp_helper <- function(to) {
if(length(to) == 0) val = ""
data.frame(to, stringsAsFactors = F)
}
pairs_df <- lst %>% sapply(tmp_helper, simplify = F) %>% plyr::ldply()
names(pairs_df)[1] <- "from"
# "dedup" the data frame to undirect the directed pairs:
undirected_pairs_df <- pairs_df %>% filter(from < to)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment