Advertisement
Guest User

Untitled

a guest
Jun 27th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. # This example shows you how to turn a JSON (and nested JSON)
  2. # response into a data.frame for further use in a R package
  3.  
  4.  
  5. sample_response <- '{
  6. "crust": "original",
  7. "toppings": ["cheese", "potatoes", "garlic"],
  8. "status": "cooking",
  9. "customer": {
  10. "name": "Garrett",
  11. "phone": "573-111-1111"
  12. }
  13. }'
  14.  
  15.  
  16. library(jsonlite)
  17. # Convert data from JSON to a list
  18. raw_data <- fromJSON(sample_response, flatten = TRUE)
  19. # Then unlist the list
  20. raw_data_csv <- unlist(raw_data)
  21. # Transpose it since we need from long to wide
  22. raw_data_csv <- t(raw_data_csv)
  23. # Now turn it into a data.frame
  24. # Be sure to set stringsAsFactors = FALSE to avoid problems in the future
  25. raw_data_csv <- data.frame(raw_data_csv, stringsAsFactors = FALSE)
  26. View(raw_data_csv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement