From c73a06c978cb44f22c3dbe4790e9dd54899143cf Mon Sep 17 00:00:00 2001 From: Jeremy Stanley Date: Mon, 6 Apr 2015 14:00:47 -0400 Subject: [PATCH] #29 improve the complex do{} example --- vignettes/introduction-to-tidyjson.Rmd | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/vignettes/introduction-to-tidyjson.Rmd b/vignettes/introduction-to-tidyjson.Rmd index 0ed168a..f98bf50 100644 --- a/vignettes/introduction-to-tidyjson.Rmd +++ b/vignettes/introduction-to-tidyjson.Rmd @@ -142,7 +142,7 @@ can parse the JSON: ```{r} library(jsonlite) # Parse the JSON into a data.frame -purch_df <- jsonlite::fromJSON(purch_json) +purch_df <- jsonlite::fromJSON(purch_json, simplifyDataFrame = TRUE) # Examine results purch_df ``` @@ -166,18 +166,14 @@ Reasoning about code like this is nearly impossible, and further, the relational structure of the data is lost (we no longer have the name of the user). We can instead try to use dplyr and the `do{}` operator to get at the -data in the nested data.frames, but this is equally challenging: +data in the nested data.frames, but this is equally challenging and confusing: ```{r} -purch_df %>% group_by(name) %>% - do( - data.frame( - name = .$name, - items = .$purchases[[1]] %>% rowwise %>% do({.$items}), - stringsAsFactors = FALSE - ) - ) %>% - summarize(price = sum(items.price)) +purch_df %>% group_by(name) %>% do({ + .$purchases[[1]] %>% rowwise %>% do({ + .$items[, "price", drop = FALSE] + }) + }) %>% summarize(price = sum(price)) ``` Using tidyjson, we can build a pipeline to turn this JSON into a tidy data.frame