Created
January 26, 2017 02:40
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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