{"action":"opened","issue":{"url":"https://api.github.com/repos/r-gris/rangl/issues/40","repository_url":"https://api.github.com/repos/r-gris/rangl","labels_url":"https://api.github.com/repos/r-gris/rangl/issues/40/labels{/name}","comments_url":"https://api.github.com/repos/r-gris/rangl/issues/40/comments","events_url":"https://api.github.com/repos/r-gris/rangl/issues/40/events","html_url":"https://github.com/r-gris/rangl/issues/40","id":195448110,"number":40,"title":"relations","user":{"login":"mdsumner","id":4107631,"avatar_url":"https://avatars.githubusercontent.com/u/4107631?v=3","gravatar_id":"","url":"https://api.github.com/users/mdsumner","html_url":"https://github.com/mdsumner","followers_url":"https://api.github.com/users/mdsumner/followers","following_url":"https://api.github.com/users/mdsumner/following{/other_user}","gists_url":"https://api.github.com/users/mdsumner/gists{/gist_id}","starred_url":"https://api.github.com/users/mdsumner/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mdsumner/subscriptions","organizations_url":"https://api.github.com/users/mdsumner/orgs","repos_url":"https://api.github.com/users/mdsumner/repos","events_url":"https://api.github.com/users/mdsumner/events{/privacy}","received_events_url":"https://api.github.com/users/mdsumner/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-12-14T06:08:23Z","updated_at":"2016-12-14T06:08:23Z","closed_at":null,"body":"\r\n---\r\ntitle: \"Structures for hierarchical data in R\"\r\nauthor: \"Michael Sumner\"\r\ndate: \"14 December 2016\"\r\noutput: html_document\r\n---\r\n\r\n```{r setup, include=FALSE}\r\nknitr::opts_chunk$set(echo = TRUE)\r\n```\r\n\r\nHierarchical data is generally hard to deal with since the layout doesn't often match the way tools must then work with the data. Physical structures are used to match the logical structure in data sets, and this is not necessarily an efficient or flexible way to deal with the data itself. \r\n\r\n** Example of extracting stuff from  a nested list**\r\n\r\n```{r}\r\nlibrary(repurrrsive)\r\ngot_chars\r\n```\r\n\r\n## Tables\r\n\r\nThe tidy framework in R generally dictates that the long form of tabular storage is the best way to work with statistical models and graphics. \r\n\r\nThe tools for converting from untidy nested structures, to long-form tables is well established, but the capacity to deal with \r\nmultiple related tables is more difficult. \r\n\r\nWhat does nesting represent? Nesting in a tree-like structure represents data as nodes in a graph, with the parent-node and child-nodes as a straightforward encoding of relations. \r\n\r\n**Tree example of a nested list**\r\n\r\n```{r}\r\n```\r\n\r\nThese relations can be encoded in \"normal-form\", where the proper entities in the data structure are stored each \r\nin a table, and the tree is not stored explicitly but as 1-many and many-1 links in relations. \r\n\r\n** Table example of a nested list **\r\n\r\n```{r}\r\n\r\n```\r\n\r\nWhen the nodes in the graph are themselves *tidy*, it's very easy to convert from the nested recursive structure to a set of tables. \r\n\r\n** Bag of relations **\r\n\r\nA list of tables doesn't seem especially helpful, and it's not  - at the user-level - but for a developer it's a very straightforward data structure to work with. \r\n\r\n## Tools to build a map of nested structure\r\n\r\n## Applying the map to extract tidy tables \r\n\r\n## Collapsing to entity tables\r\n\r\n\r\n```{r}\r\nlibrary(repurrrsive)\r\n\r\nlibrary(purrr)\r\nlibrary(tibble)\r\nlibrary(dplyr)\r\nlibrary(tidyr)\r\nmap_df(wesanderson, function(x) tibble(color = x), .id = \"palette\")\r\n\r\nmap(got_chars[[1]], length)\r\n\r\n\r\nlibrary(sf)\r\nnc <- st_read(system.file(\"shape/nc.shp\", package=\"sf\"))\r\nmap(st_geometry(nc), length)\r\n\r\nnc %>% select(CNTY_ID, AREA, geometry) %>% mutate(len = at_depth(geometry, 1, length)) %>% filter(len > 1)\r\n\r\nshp <- as(nc, \"Spatial\")\r\nlibrary(spbabel)\r\nlibrary(sp)\r\nmap_table(nc)\r\n\r\n## break down nc and rebuild as tibble\r\ntnc <- nc %>% select(-geometry) %>% as_tibble()\r\ntnc$geometry <- st_geometry(nc)\r\nst_as_sf(tnc) %>% by_row(map_table)\r\nmap_table.tbl_df <- function(x, ...) {\r\n  map_table(st_as_sf(x))\r\n}\r\n\r\nx <- st_as_sf(tnc) %>% by_row(map_table) %>% select(.out)\r\n```\r\n\r\n\r\n## Examples with spatial data\r\n\r\n** Normal form for simple features **\r\n\r\n** Primitives form (topology) **\r\n\r\n** Recording aesthetics as variables with primitives **\r\n\r\n** Recording variables generally on entities **\r\n\r\n** Primitives from ggvis ** \r\n"}}
{"actor":{"display_login":"mdsumner"}}