Consider this example from the tidyjson
GH page:
#devtools::install_github("sailthru/tidyjson")
library(tidyjson)
library(dplyr)
json <- '[{"name": "bob", "age": 32}, {"name": "susan", "age": 54}]'
json %>%
as.tbl_json %>%
gather_array %>%
spread_values(
name = jstring("name"),
age = jnumber("age")
)
# A tibble: 2 x 4
document.id array.index name age
* <int> <int> <chr> <dbl>
1 1 1 bob 32
2 1 2 susan 54
I have extracted all JSON keys and put it into a vector. I am looking for an efficient solution in the vein of (just jstring
is fine) to create a tabular dataset:
features <- c("name", "age", "gender", "education", "income", ...)
# spread_values(features = jstring(features))
How do I generalise the list at the spread_values
step; i.e. in the above example, how can I condense name
and age
to a one-liner? The name of the columns are always the same as the JSON keys. I have tried apply family (lapply
/sapply
) functions and a for-loop, but always get the same assignment error:
Error: unexpected '=' in...
Cheers.