expand_cat_json | R Documentation |
This function accepts a data.table
along with a set of grouping variables and a character-format category-style JSON column (i.e. starts with curly brackets, not square).
expand_cat_json( dt, expand, index = "index", by = NULL, fast = FALSE, fun = sum, na.rm = TRUE, set_key = TRUE )
dt |
data.table object (or something that can be coerced to data.table) |
expand |
String indicating the JSON column to be expanded. |
index |
String indicating the name of the new index column |
by |
Character vector indicating the variables to group by after expanding. Set to |
fast |
Assumes that all the JSON vectors are of the exact same categories, and adds the values together rather than using whatever is in |
fun |
Function that takes a vector and returns a single value to use when collapsing to the |
na.rm |
Ignore missing values of |
set_key |
Set the key of |
It expands that JSON column into long format, with one row per observation per value of the JSON column, and then collapses everything according to the set of grouping variables.
# Raw example data for expanding/collapsing patterns <- data.table::data.table(state_fips = c(1,1,2,2), cat_origin = c('{"a": "2", "b": "3"}', '{"a": "3", "b": "4"}', '{"a": "4", "b": "5"}', '{"a": "5", "b": "6"}')) expand_cat_json(patterns, 'cat_origin', by = 'state_fips')[]
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.